Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Semaphores: Producer Consumer problem We are going to use 1 producer thread and

ID: 3643221 • Letter: S

Question

Semaphores: Producer Consumer problem

We 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

Explanation / Answer

Without semaphore 01 package main 02 03 import ( 04 "log" 05 "os" 06 "rand" 07 "time" 08 ) 09 10 var fmt = log.New(os.Stdout, "", log.Lmicroseconds) 11 12 func main() { 13 rand.Seed(time.Nanoseconds()) 14 15 // bounded buffer 16 buf := make(chan int, 3) 17 18 // producer 19 go func() { 20 for i := 0; i < 10; i++ { 21 time.Sleep(rand.Int63n(7e7)) 22 fmt.Println("produced", i) 23 select { 24 case buf
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote