Lets say that the Wait and Signal operations are implemented as shown
in the pseudocode given below :
Wait (Semaphore S)
while (S.count <= 0)
{// Loop as long as S.count <= 0}
S.count = S.count - 1
Signal (Semaphore S)
S.count = S.count + 1
Assume that the semaphore is initialized with count 1.
Let there be two processes ( P1 & P2 excecuting). Then, as per the
timeline given below, mutual exclusion will be violated.
Time Events
---- ------
T0 : S.count = 1. P1 calls Wait, executes the while loop, and breaks
out because count is positive. Then a context switch occurs to T2
before T1 can decrement count.
T1 : S.count = 1. P2 calls Wait, executes the while loop, decrements
count, and returns and enters the critical section. Then a context
switch occurs to T1.
T2 : S.count = 0. P1 decrements count, and also enters the critical
section.
At time T2, both the processes T1 and T2 are in the critical section
and the mutual exclusion is therefore violated as a result of a lack
of atomicity
Hope this helps.
If you need any clarifications, just ask!
:) |