Lab Assignment--Threads and Mutexes


(Please do this lab in LINUX)

This week you will write a program that creates 3 threads. These threads will access a shared resource, an integer called buffer, one at a time. The buffer will initially be set to '0'. They will each print their thread ID, process ID and the buffer's current value (translated to a letter) in one statement, then increment the buffer by one. Use a mutex to ensure this whole process is not interrupted.

Have the threads modify the buffer a total of 24 times. When each thread is done, it should return the number of times it modified the buffer to the main thread. The total number of modifications should be exactly 24.
(Hint: the buffer may increase in value while you are waiting to access it. Make sure that you have code that checks the buffer's value after you get access, but before you work with it.)

The threads do not need to access the buffer in any particular order, but over multiple runs it should be obvious that all three are getting a fair chance. If one buffer seems to dominate over multiple runs, try introducing a sleep to simulate work. Try it in each of the following positions: (1) between the mutex lock and unlock, (2) just before the lock, or (3) just after the unlock.
(Tip: if sleep is too slow for you, try nanosleep. A sample usage is: nanosleep((const struct timespec[]){{0, 5000L}}, NULL);)

Details

Sample runs:

$ ./exercise 
TID: 3077897072 PID: 30656 Buffer: A
TID: 3069504368 PID: 30656 Buffer: B
TID: 3059014512 PID: 30656 Buffer: C
TID: 3077897072 PID: 30656 Buffer: D
TID: 3069504368 PID: 30656 Buffer: E
TID: 3077897072 PID: 30656 Buffer: F
TID: 3059014512 PID: 30656 Buffer: G
TID: 3069504368 PID: 30656 Buffer: H
TID: 3077897072 PID: 30656 Buffer: I
TID: 3059014512 PID: 30656 Buffer: J
TID: 3069504368 PID: 30656 Buffer: K
TID: 3077897072 PID: 30656 Buffer: L
TID: 3069504368 PID: 30656 Buffer: M
TID: 3059014512 PID: 30656 Buffer: N
TID: 3069504368 PID: 30656 Buffer: O
TID: 3077897072 PID: 30656 Buffer: P
TID: 3059014512 PID: 30656 Buffer: Q
TID: 3077897072 PID: 30656 Buffer: R
TID: 3069504368 PID: 30656 Buffer: S
TID: 3059014512 PID: 30656 Buffer: T
TID: 3077897072 PID: 30656 Buffer: U
TID: 3069504368 PID: 30656 Buffer: V
TID: 3059014512 PID: 30656 Buffer: W
TID: 3077897072 PID: 30656 Buffer: X
TID 3077897072 worked on the buffer 9 times
TID 3069504368 worked on the buffer 8 times
TID 3059014512 worked on the buffer 7 times
Total buffer accesses: 24

$ ./exercise 
TID: 3077978992 PID: 30660 Buffer: A
TID: 3069586288 PID: 30660 Buffer: B
TID: 3059096432 PID: 30660 Buffer: C
TID: 3077978992 PID: 30660 Buffer: D
TID: 3069586288 PID: 30660 Buffer: E
TID: 3077978992 PID: 30660 Buffer: F
TID: 3059096432 PID: 30660 Buffer: G
TID: 3077978992 PID: 30660 Buffer: H
TID: 3069586288 PID: 30660 Buffer: I
TID: 3077978992 PID: 30660 Buffer: J
TID: 3059096432 PID: 30660 Buffer: K
TID: 3077978992 PID: 30660 Buffer: L
TID: 3069586288 PID: 30660 Buffer: M
TID: 3077978992 PID: 30660 Buffer: N
TID: 3059096432 PID: 30660 Buffer: O
TID: 3077978992 PID: 30660 Buffer: P
TID: 3069586288 PID: 30660 Buffer: Q
TID: 3077978992 PID: 30660 Buffer: R
TID: 3059096432 PID: 30660 Buffer: S
TID: 3077978992 PID: 30660 Buffer: T
TID: 3069586288 PID: 30660 Buffer: U
TID: 3077978992 PID: 30660 Buffer: V
TID: 3059096432 PID: 30660 Buffer: W
TID: 3077978992 PID: 30660 Buffer: X
TID 3077978992 worked on the buffer 12 times
TID 3069586288 worked on the buffer 6 times
TID 3059096432 worked on the buffer 6 times
Total buffer accesses: 24

Deliverables:

Submit 2 files to URCourses:

  1. The code
  2. A few sample runs