---
title: "Mutual exclusion via sleep and wake"
subject: "Embedded System"
chapter: 6
key: "semester_6/embedded_system:mutual-exclusion-sleep-wake"
source: https://www.bctengineer.com/q/semester_6/embedded_system/mutual-exclusion-sleep-wake
site: BCT Engineering
---

# Mutual exclusion via sleep and wake

Embedded System · Chapter 6: RTOS

- Asked in: 2075 Bhadra, 4 marks
- Video: [Synchronization Mechanism ll Busy Waiting and Without Busy Waiting ll Explained in Hindi](https://www.youtube.com/watch?v=uNjRFUmqya8) — 5 Minutes Engineering
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/mutual-exclusion-sleep-wake
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-6

## Question

In RTOS, describe mutual exclusion through sleep and wake for task synchronization.

## Short answer

Sleep/wake mutual exclusion puts a task to sleep when a resource is locked, queuing it, and wakes it upon release, eliminating CPU waste. The sleep-and-test must be atomic to avoid lost wake-ups.

## Answer

**Mutual exclusion** means that only one task at a time may execute its critical section (code using a shared resource). Busy waiting does this by spinning and wasting CPU. **Sleep and wake** solves this by putting the waiting task to sleep (blocked state) so it uses no CPU, and waking it when the resource becomes free.

#### Operation
1. A task wants to enter the critical section and finds the resource locked by another task.
2. Instead of looping, it calls **sleep()** (block): the kernel moves it to the blocked state, adds it to the resource's wait queue and runs another ready task.
3. When the owner finishes the critical section and releases the resource, it calls **wakeup()** for the waiting task.
4. The kernel moves the woken task to the ready state; when scheduled, it enters the critical section.

```c
void enter(void)                 /* acquire */
{
    disable_interrupts();
    if (locked) { add_to_wait_queue(me); sleep(); }
    else          locked = 1;
    enable_interrupts();
}
void leave(void)                 /* release */
{
    disable_interrupts();
    if (queue_not_empty()) wakeup(remove_from_queue());
    else                   locked = 0;
    enable_interrupts();
}
```
Semaphores and mutexes are the standard implementations of sleep/wake.

**Advantages:** no CPU wasted, fair order through the queue, suits long waits. **Problem to avoid:** *lost wake-up* (a wakeup sent before the task goes to sleep is lost), solved by testing and sleeping atomically, as semaphores do. Also priority inversion and deadlock must be considered.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does mutual exclusion guarantee?
   A. All tasks execute their critical section together
   B. Tasks never block
   C. Tasks run in priority order always
   D. Only one task at a time executes its critical section
   Answer: D. It protects shared resources by allowing a single task inside the critical section. (Key point: Mutual exclusion)

2. What happens when a task finds the resource locked under sleep/wake?
   A. It spins on the lock
   B. It is deleted
   C. It is moved to the blocked state and added to the wait queue
   D. It forces the owner to release the lock
   Answer: C. Sleep blocks the task, so it uses no CPU while another ready task runs. (Key point: Sleep action)

3. What does the owner do on leaving the critical section?
   A. Calls sleep() on itself
   B. Calls wakeup() so the waiting task becomes ready
   C. Disables the waiting task
   D. Resets the system tick
   Answer: B. The kernel moves the woken task to ready, and it enters the critical section when scheduled. (Key point: Wake action)

4. What is the lost wake-up problem and its remedy?
   A. A wakeup sent before the task sleeps is lost; test and sleep atomically as semaphores do
   B. A task wakes twice; use more flags
   C. A task never sleeps; use busy wait
   D. The queue overflows; use bigger memory
   Answer: A. Making test-and-sleep atomic ensures a wakeup cannot slip in between. (Key point: Lost wake-up)

## Related in Embedded System

- [Task synchronization using busy/wait](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Atask-sync-busy-wait) — Embedded System, chapter 6
- [Synchronization issues in RTOS](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Artos-synchronization-issues) — Embedded System, chapter 6, asked 2×

## Related in other subjects

- [Ricart-Agrawala mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aricart-agrawala) — Distributed System, chapter 6, asked 2×
- [Token ring mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Atoken-ring-mutex) — Distributed System, chapter 6, asked 3×
- [Lamport algorithm for mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alamport-mutex) — Distributed System, chapter 6
- [Critical section; mutual exclusion](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Acritical-section-mutual-exclusion-pc) — Operating System, chapter 3
