Operating Systems · Semaphores and Mutex
Sleep and Wake Up Mechanism
Overview
Sleep and Wake Up is a synchronisation mechanism that replaces busy waiting with blocking.
A process that cannot enter the critical section goes to sleep instead of repeatedly testing the lock, and the process leaving the critical section wakes a sleeping process.
This provides mutual exclusion without wasting CPU cycles and also removes priority inversion.
Key concepts
- Busy waiting (spinning): A process that finds the critical section occupied keeps checking the condition again and again in a loop — it keeps "knocking on the door".
The process inside is in no hurry to come out, so the waiting processes repeatedly test the lock. This continuous checking is the defect of busy waiting. - Sleep: If a process wants a resource or the critical section and it is not available, that process goes and sleeps — it is blocked, not spinning.
- Wake up: As soon as the process using the critical section finishes its usage, it goes and wakes up the sleeping process, and that process is again sent to the critical section.
- Mutual exclusion without busy waiting: The sleeping process does not consume CPU cycles while waiting, so the CPU is never kept blocked or idle by useless checking.
- Priority inversion removed: Whenever a lower-priority process is in the critical section, it is pulled out and put to sleep, so the higher-priority process can get inside.
How it works
- A process wants to enter the critical section.
- It checks whether the critical section is free.
- If it is free, the process enters and executes the critical section.
- If it is not free (another process is already inside), the process does not keep checking in a loop — it goes to sleep.
- When the process inside the critical section finishes its usage, it comes out and wakes up one of the sleeping processes, which is then sent into the critical section.
Drawing the diagram…
Worked examples
Not applicable — the input contains no numerical example.
Comparison
| Aspect | Busy waiting (continuous knocking) | Sleep and Wake Up |
|---|---|---|
| Behaviour of waiting process | Keeps checking / "knocking" repeatedly in a loop | Goes to sleep (blocks) and waits to be woken |
| CPU utilisation | CPU is wasted in repeated checking | CPU is not wasted; no busy waiting |
| Who acts | Waiting process itself keeps retrying | The process leaving the critical section wakes up a sleeping process |
| Priority inversion | Problem arises | |
| Mutual exclusion | Provided | Provided, but without busy waiting |
Revision summary
- Continuous checking of a lock by a waiting process ("knocking on the door") is busy waiting — the problem to be eliminated.
- Core idea: do not spin — sleep; the process that exits the critical section performs the wake up.
- Sleep and wake up give mutual exclusion without busy waiting, so the CPU is never wasted in useless checking.
- Priority inversion caused by busy waiting is avoided: a higher-priority process waiting for the critical section sleeps instead of spinning, so the lower-priority process inside can run and leave.
- Sleep = block when the resource / critical section is not available;
wake up = the process finishing the critical section wakes a sleeping process and sends it into the critical section. - The exiting process wakes up exactly one sleeping process.
Previous-year exam insights
- Sleep/wake up implemented with semaphores uses two operations: block (sleep) on a semaphore, which puts the calling process in the semaphore's waiting queue and changes its state to blocked/waiting, and wakeup, which removes one process from the queue and changes it to ready.
Asked inGATE 2006 - A semaphore's wait/P operation decrements the semaphore value; if the result is negative the process blocks (sleeps) and is added to the semaphore's queue.
The signal/V operation increments the value; if the result is less than or equal to zero it wakes up one blocked process from the queue.
Asked inGATE 2006 - Monitor condition variables have no value; wait(x) on a condition variable always blocks the calling process and releases the monitor lock, while signal(x) wakes exactly one process waiting on x (no effect if none is waiting).
Asked inGATE 2006 - In monitor implementations with semaphores, a process that signals a condition variable may need to wait (using a 'next' semaphore) until the awakened process leaves the monitor, and the count of such waiting signalers must be maintained.
Asked inGATE 2006 - Sleep/wake up primitives can suffer from the lost-wakeup problem: if a wakeup is issued before the other process actually sleeps, the wakeup is lost and the sleeper may block forever; semaphores avoid this by recording the signal in the semaphore value.
- Busy waiting (spinlock) is preferable only when the expected wait is shorter than the cost of two context switches (e.g., on multiprocessors); otherwise blocking (sleep/wake up) is more efficient.