In this assignment you will experiment with creating threads and with thread synchronization using mutex locks.
Part 0:
Make a clean direcotry called assign4 and copy the following files
into this directory:
Modify countit0.c so that it prints your name instead of mine. Use the makefile to lint, compile and run the program on a Sun machine. Label and save the lint output and the output of the program. Now compile and run the program on a Linux machine. You should execute make clean each time you change which system you are running on. Label and save your output. Try to understand why you got the output you did.
Part 1:
Copy countit0.c to countit1.c and add countit1 to
the first line of the makefile. Modify the count0 function so that
instead of executing counter++ it does this in three steps:
temp = counter; temp = temp + 1; counter = temp;Again, lint, compile and run on a Sun machine and compile and run on a Linux machine. Label and save the output. Try to understand why you got the output you did.
Part 2:
Copy countit1.c to countit2.c and add countit2 to
the first line of the makefile. Modify the count0 function so that
it executes the following line after setting temp to counter:
for (j=0;j<1000;j++) ;Again, lint, compile and run on a Sun machine and compile and run on a Linux machine. Label and save the output. Try to understand why you got the output you did.
Part 3:
Copy countit1.c to countit3.c and add countit3 to
the first line of the makefile. Modify the count0 function so that
it executes the following line after setting temp to counter:
yield_thread(YIELDPROB);Again, lint, compile and run on a Sun machine and compile and run on a Linux machine. Label and save the output. Try to understand why you got the output you did.
Part 4:
Copy countit3.c to countit4.c and add countit4 to
the first line of the makefile. Modify the count0 function so that
it uses a mutex to protect the modification to the counter
variable. Both the lock and the unlock of the mutex should be inside the
for loop. Check for errors on the mutex functions.
For this assignment, if an error occurs, print a message to standard error and
exit the thread (not the program).
Define the mutex inside the count0 function.
Again, lint, compile and run on a Sun machine and compile and run on a
Linux machine. Label and save the output.
Try to understand why you got the output you did.
Part 5: (extra credit)
Copy countit2.c to countit5.c and add countit5 to
the first line of the makefile. Try to find a method of estimating
the number of context switches between the different threads.
Modify the program so that
it prints this number after all threads have completed. Try not to make too
much of a change to the count0 function so that this program
behaves like the one in Part 2.
Again, lint, compile and run on a Sun machine and compile and run on a
Linux machine. Label and save the output.
Try to understand why you got the output you did.
Handing in your program:
Use this cover sheet. Consecutively number all of
the other pages you turn in. Each page that contains your work should have
a number on it. Turn this in at the beginning of lecture on the due date.
For each part, the cover sheet lists a page number of an explanation.
Write a short explanation of why you think you got the output you did.
If you did Part 5, explain in words the method you used to determine
when a context switch took place.