CSCI 4061 Lab11: Threads and Worms
- Due: 11:59pm Monday 4/21/2019
- Approximately 0.83% of total grade
- Submit to Canvas
- Lab exercises are open resource/open collaboration. You must submit your own work but you may freely discuss lab topics with other members of the class.
CODE DISTRIBUTION: lab11-code.zip
- Download the code distribution every lab
- See further setup instructions below
CHANGELOG: Empty
1 Rationale
Basic working knowledge of a threading library is an essential element in any system programmers toolkit. This lab is focused on the basics of the pthreads (POSIX Threads) library due to its widespread availability. This lab introduces basic thread usage via a simple visual demo where threads run at different rates. Basic mechanics such as passing threads initialization arguments and utilizing mutexes to avoid race conditions are demonstrated. Completing the lab will give basic knowledge of the pthreads library and use of mutexes to coordinate multiple threads.
1.1 Associated Reading
- Ch 11-12 of Stevens & Rago discusses Thread creation and coordination. Ch 11.4-11.6 are particularly relevant with 11.6 specifically discussing mutexes.
- Ch 12-13 of Robbins & Robbins covers similar material with 13.2 covering mutexes.
1.2 Grading Policy
- 30% of lab credit is earned by showing your lab leader answers during lab
- 70% of lab credit is earned by submitting required files
See the full policy in the syllabus.
2 Codepack
The codepack for the lab contains the following files:
File | Description |
---|---|
Makefile |
Makefile to build programs below |
worms_pthread.c |
Code to analyze for problems |
worm_board.txt |
Text file create |
worms.py |
Optional file to examine |
3 Questions
Analyze these files and answer the questions given in QUESTIONS.txt
.
__________________ LAB 11 QUESTIONS __________________ - Name: (FILL THIS in) - NetID: (THE kauf095 IN kauf0095@umn.edu) Answer the questions below according to the lab specification. Write your answers directly in this text file and submit it to complete Lab01. PROBLEM 1: The Setup ==================== A ~ Compile and run `worm_pthread.c' using the provided Makefile. Make sure to follow the program prompts. Describe what you observe. Also do brief research and find out what the Unix command `watch' does and how it is used with this program. B ~ Consult the structure of the `main()' function. Determine how changes to the file `worm_board.txt' are made. What special tricks are used to make it relatively easy to change the file AND to change specific portions of the file. C ~ Describe the area of code in `main()' which creates threads and awaits them finishing. - Describe the function which is used to start a thread running and its arguments. Consult the manual for documentation on this function if needed. - Describe the function which is used to wait until a thread finishes. PROBLEM 2: The Worms ==================== A ~ Examine the `worm_func()' function carefully. You may wish to consider the specific data passed to these worms which are in the array of `wormparam_t' structs midway through `main()'. Describe the basic algorithm that worms follow to do their work. B ~ Describe how worms avoid both claiming the same piece of territory. What system call mechanism is used to ensure that two worms cannot claim the same area of the board? Describe the function calls that are used to accomplish this and what functions in `main()' are used to set up this coordination mechanism. C ~ Describe the logic that appears to cause worms to be 'fast' and 'slow': how is this artificial speed actually created in the `worm_func()' code. While the speed differences of worms is an intended creation of the program, speculate as to what use threads can be when dealing with entities of different speeds. Optional Enrichment: Threads in Python ====================================== Threads are not unique to C and the Pthreads library. Most modern programming environments include some version of them. The prototype version of the worms program was written in Python and can be found in `worms.py' and can be run via ,---- | > ./worms.py `---- - No territory or step numbers are tracked in the Python version; it runs until Ctrl-c is pressed - This version does not use a memory mapped file instead showing the board on the console. - The `curses' library is used to manipulate the terminal screen which allows for boxes and color to be used but complicates the implementation somewhat. - Python features several types of mutual exclusion locks including one used here that can be re-locked by the same thread without negative effects. Time permitting, explore the code of `worms.py' and draw some parallels as to the C code.
4 What to Understand
Ensure that you understand
- The function calls to create threads and wait for the completion of
another thread along with
structs
used for this purpose. - The function calls used to set up and clean up mutexes along with
associated
structs
- Calls which lock and unlock mutexes
5 What to Turn In
Submit your completed QUESTIONS.txt
file to Canvas under the
appropriate lab heading. Make sure to paste in any new code you were
to write at appropriate locations in the file.