What will happen when two processes execute wait() operation on
the
same semaphore at the same time and wait() operation is not
atomic?
S=1
wait(S)
S){
while (S <= 0)
; // busy wait
S
}
Describe a scenario of context switches where two threads, T 1 and
T 2 ,
can both enter a critical section guarded by a single mutex
semaphore
as a result of a lack of atomicity.
Let's say you have 2 threads T1 and T2 and a semaphore with count of 1.
If they both down() at the same time, the atomicity of the primitive guarantees that one will be granted the semaphore and the other one will go to sleep. In particular it is impossible for both to decide to go to sleep OR both acquiring it.
Similarly, down() vs up(). up() will release and wakeup as necessary. In particular it is impossible for the thread doing down() to go to sleep after up() released it.
It's the entire point of the primitive.
Get Answers For Free
Most questions answered within 1 hours.