How do the wait() and signal() operations of a semaphore work internally, and why must they be atomic?
Last updated
A semaphore is a protected integer counter.
wait()(P) decrements it and blocks the caller on a wait queue if the result is negative.signal()(V) increments the counter and, if processes are waiting, moves one from the queue to ready.Both must be atomic because the update is a read-modify-write: interleaving could let two processes past a mutex (lost update). Atomicity comes from disabling interrupts (uniprocessor) or
Test-and-Set/Compare-and-Swap(multiprocessor).
- https://dev.to/endeavourmonk/semaphore-in-operating-system-with-its-own-implementation-in-c-19ia
- https://medium.com/@arundhatiarjaria07/understanding-semaphores-in-operating-systems-cece2180c20b
- https://www.geeksforgeeks.org/operating-systems/semaphores-and-its-types/
- https://www.geeksforgeeks.org/operating-systems/semaphores-solutions-in-operating-system/