Semaphores: Producer Consumer problem We are going to use 1 producer thread and
ID: 3643221 • Letter: S
Question
Semaphores: Producer Consumer problemWe are going to use 1 producer thread and 5 consumer threads. They are going to run forever. There are two versions of the algorithm:
1. without using semaphores (proj7v1.cpp)
2. with semaphores (proj7v2.cpp)
Submit both programs with sample outputs for both. Try to highlight the differences between both outputs. Feel free to insert sleep() in appropriate places to enable strange behavior. Try to keep those sleep() calls in both programs too.
Use following guidelines for the assignment:
Bounded Buffer - b[1] b[2] ... b[n]
Block on: Producer: insert in full buffer
Consumer: remove from empty buffer
Unblock on: Consumer: item inserted
Producer: item removed
***************************
Producer:
while (true) {
//produce item v
while ((in + 1) % n == out)
append(); //do nothing
}
Consumer:
while (true) {
while (in == out) //buffer is empty
w = take(); //consume item w
}
Semaphore n = 0, p = 1, c = 1, e = sizeOfBuffer
Producer:
while (true) {
v = produce();
semWait(e);
append(v);
semSignal(n);
}
Consumer:
while (true) {
semWait(n);
semWait(c);
w = take();
semSignal(c);
semSignal(e);
consume(w);
}
********************
append(v): b[in] = v; in = (in + 1) % n;
take(): w = b[out]; sleep(i); out = (out + 1) % n; return w;
Array b[] can be an array of 20 integers. produce() can be as simple as return the next integer in the sequence starting with integer value 1. consume(w) can simply print w out.
Please submit proj7v1.cpp, proj7v2.cpp, buffer.h, buffer.cpp, output1.txt, output2.txt