The following program consists of concurrent processes and binary semaphores. The semaphores are initialized as and
How many times will process print ''?
View worked solution
Answer: A
To determine how many times process prints '0', let us trace the execution of the concurrent processes with binary semaphores initialized to .
Initial State: .
- executes
wait(S0), decrements to , prints '0' (1st print), and executesrelease(S1)andrelease(S2).
Current state: .
Since and are non-looping processes, they can each execute their release(S0) at most once. Depending on the scheduling order, we have two possible cases:
- Case 1 (Interleaved execution - 3 prints):
- runs:
wait(S1), thenrelease(S0). - runs:
wait(S0), prints '0' (2nd print), and sets . - runs:
wait(S2), thenrelease(S0). - runs:
wait(S0), prints '0' (3rd print), and sets . - loops and blocks on
wait(S0). Since both and have terminated, is blocked forever. Total prints = 3.
- runs:
- Case 2 (Consecutive execution - 2 prints):
- runs:
wait(S1), thenrelease(S0). - runs:
wait(S2), thenrelease(S0). Since is a binary semaphore and already , it remains . - runs:
wait(S0), prints '0' (2nd print), and sets . - loops and blocks on
wait(S0). Since both and have terminated, is blocked forever. Total prints = 2.
- runs:
Since the minimum number of prints is 2 and the maximum is 3, process will print '0' at least twice.
Free practice · Your answers on this page are not saved.
