I was studying binary semaphores when the following question turned up:
Suppose there are 3 concurrent processes and 3 binary semaphores... The semaphores are intitialised as S0=1, S1=0, S2=0. The processes have the following code:
Process P0: Process P1: Process P2:
while (true){ wait(S1); wait(S2);
wait (S0); release (S0); release(S0);
print '0';
release (S1);
release (S2);
}
Now the question is how many times the process will print 0 ?
Let me explain How i was solving it.. suppose the first three statements of the three processes be executed concurrently! i.e, the while statement of process p0, wait(S1) of process p1 and wait(S2) of process P2.. Now, the wait(S1) and wait(S2) will both make the semaphore values -1 and the processes P1 and P2 will be blocked.. Then wait(S0) of Process P0 will be executed. When this happens S0's value becomes 0 and the process P0 moves into blocked state, as a result all the processes will be blocked and in a deadlock state!! But unfortunately thats not the answer. . Please tell me where I am wrong and how the solution proceeds ? :|
EDIT:
I was wrong in my approach to binary semaphores.. they can take only 0 and 1!