Using the threads, you have implemented, implement semaphores. Since the threads are nonpreemptive, you do not need to ensure atomicity of the semaphores (they are already atomic).
Implement the following: (in a file called sem.h)
- Semaphore data structure: A value field and a queue of TCBs.
- InitSem(semaphore, value): Initializes the value field with the specified value.
- P(semaphore): The P routine decrements the semaphore, and if the value is less than zero then blocks the process in the queue associated with the semaphore.
- V(semaphore): The V routine increments the semaphore, and if the value is 0 or negative, then takes a PCB out of the semaphore queue and puts it into the run queue. Note: The V routine also “yields” to the next runnable process. //this is important.
- Implement a solution to the Producer Consumer Problem with the following settings:
There are three producers all in infinite while loops each producing 1 item in one loop.
There are three consumers all in infinite while loops each consuming 1 item in one loop.
Every consumer or producer yields at the end of a loop to the next process.
Consider that the buffer size is 6 items.
Consider that in your ready queue there are three consumers then seven producers then 7 consumers and finally three more producers .
Run the code for infinitely long.
At the end of each loop before yield the consumer should print the following: if consumer X is consuming an item then print
“This is consumer X consuming item generated by producer Y” else print
“Consumer X is waiting”
At the end of each loop before yield the producer should print the following:
if producer X is producing an item then print “This is producing X producing item number Y” else print
“Producer X is waiting”
Submission and Grading:
Your project must consist of 4 files
- h (uses ucontext.h)
- h (includes TCB.h)
- h (includes q.h)
- h (includes threads.h)
- proj-3.c (includes threads.h) – must contain your name(s) in comments @ beginning (make sure the compile command, “gcc proj-3.c” does the correct compilation).
All 5 files are to be ZIPPED into one zip or gzip file and uploaded in Canvas. The name of this file should reflect the name of the student (abbreviated, do not make it too long).
Note: Grading is on Ubuntu. It is in your interest to make sure the program compiles and runs in the target test platform.