A shared variable , initialized to zero, is operated on by four concurrent processes as follows. Each of the processes and reads from memory, increments by one, stores it to memory, and then terminates. Each of the processes and reads from memory, decrements by two, stores it to memory, and then terminates. Each process before reading invokes the operation (i.e., wait) on a counting semaphore and invokes the operation (i.e., signal) on the semaphore after storing to memory. Semaphore is initialized to two. What is the maximum possible value of after all processes complete execution?
View worked solution
Answer: D
Since the counting semaphore is initialized to , at most two processes can enter their critical sections concurrently. This concurrency allows for "lost updates" when two processes read the same value of before either writes it back.
To maximize the final value of , we want the decrement operations of and to be overwritten (lost) by the increment operations of and . We can achieve this with the following sequence starting with and :
- Phase 1 (Overwriting 's decrement):
- and enter the critical section ( becomes ).
- reads .
- reads .
- decrements to , writes , and exits ( becomes ).
- increments its local copy (), writes , and exits ( becomes ).
- Phase 2 (Overwriting 's decrement):
- and enter the critical section ( becomes ).
- reads .
- reads .
- decrements to , writes , and exits ( becomes ).
- increments its local copy (), writes , and exits ( becomes ).
After all processes terminate, the maximum possible value of is .
Free practice · Your answers on this page are not saved.
