2. (6 points) In the following code, three processes are synchronized using two
ID: 3919006 • Letter: 2
Question
2. (6 points) In the following code, three processes are synchronized using two semaphores L and R and produce output using the routine putc semaphore L.value = 3, R.value = 0; /* initialization */ /Process 1 L1 : /Process 2 L2 : /Process 3 L3: wait (L) putc('C signal (R) wait (R) putc 'A'; putc('B' signal (R) goto L2; wait (R) putc'D' goto L3; goto Ll; (a) How many D's are printed when these processes run? (b) Is CABABDDCABCABD a possible output sequence when these processes run? (c) Is CABACDBCABDD a possible output sequence when these processes run?Explanation / Answer
a) Three times.
Explanation:
Each time Process 1 executes the "wait(L)" statement, the value of semaphore L is decremented by 1. Since there are no "signal(L)" statements, the loop in Process 1 will execute only 3 times (ie, the initial value of L) and then stall the fourth time "wait(L)" is executed.
Process 1 will execute its loop 3 times (as explained above), by incrementing "signal(R)" each time through the loop. This will allow "wait(R)" to be completed for three times. For every "wait(V)" Process 2 executes, it also executes a "signal(R)" so there is no change in the value of semaphore V caused by Process 2. Process 3 does decrement the value of semaphore V, typing out "D" each time it does so. So , Process 3 will eventually loop as many times as Process 1.
b) No. Because
start: L=3 R=0
type C: L=2 R=1
type A: L=2 R=0
type B: L=2 R=1
type A: L=2 R=0
type B: L=2 R=1
type D: L=2 R=0
type D: impossible as R=0
c) Yes. Because
start: L=3 R=0
type C: L=2 R=1
type A: L=2 R=0
type B: L=2 R=1
type A: L=2 R=0
type C: L=1 R=1
type D: L=1 R=0
type B: L=1 R=1
type C: L=0 R=2
type A: L=0 R=1
type B: L=0 R=2
type D: L=0 R=1
type D: L=0 R=0
Thank You! I hope it helped. :-)
If any query provide a feedback.
Please thumbs up if you liked.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.