Objectives:
- Use Pthreads
- Synchronize thread activity
Assignment
Create a program in C using Pthreads that has
N counters that are incremented by
N + 1 worker threads. Each thread increments each counter 100,000 times by taking the sum of the other counters modulo the sum of its thread index plus
N.
Each counter needs its own synchronization primitive.
Threads cannot increment the same counter twice in a row and must release all synchronization primitives before incrementing another counter.
Initialize the first counter to 0, the second counter to 1, the third counter to 2, etc.
Use
N = 4.
Finally, the main thread reports the total that each worker thread incremented the counters by.
Example
Thread number 1 contributed a total of 595418 increments
Thread number 2 contributed a total of 800500 increments
Thread number 3 contributed a total of 995315 increments
Thread number 4 contributed a total of 1199346 increments
Thread number 5 contributed a total of 1394445 increments
Counters' values:
1131220
1112770
1130862
1089657
Note: Do NOT try to match these values exactly.
Rubric:
Points Item
---------- --------------------------------------------------------------
_____ / 10 Documentation
Header comment block at the beginning of each file with:
+ Your full name
+ Date(s) code was written
+ Description
Comments explaining the role of each variable and major section of code
Correctness - Program solves the assigned problem using methods described in program description
_____ / 0 Program compiles without errors
_____ / 0 Program executes without crashing
_____ / 25 Threads (creation, joining, etc.)
_____ / 18 Synchronization primitives (one per counter)
_____ / 10 Avoids deadlock
_____ / 2 Completed rubric (estimates for each line including hours spent)
_____ / 65 Total
_____ Approximate number of hours spent
Submission
Before submitting this assignment, make sure there are no compilation errors. If there are, fix them before submitting.
You may need to edit and compile your program multiple times before it compiles successfully.
Verify that the output is correct.
Once you are able to successfully run the program with the correct output, you are ready to submit the program.
Submit your
project3-yourlastname.c and
rubric-yourlastname.txt to the Assignments tab in CougarVIEW.
(Note: Do not submit files with spaces in their names.)
Notes
- If you choose to use semaphores, due to the execution test environment, unnamed semaphores are not allowed (i.e., sem_init() and sem_destroy()). Use named semaphores instead (sem_open() and sem_close()).