---
title: "Race condition; sleep() and wakeup()"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:race-sleep-wakeup"
source: https://www.bctengineer.com/q/semester_6/operating_system/race-sleep-wakeup
site: BCT Engineering
---

# Race condition; sleep() and wakeup()

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2074 Bhadra, 2+3 marks
- Video: [Sleep and Wakeup ll Producer and Consumer Problem ll Operating System Explained in Hindi](https://www.youtube.com/watch?v=zMYhslEjVok) — 5 Minutes Engineering
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/race-sleep-wakeup
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

What is race condition? Explain how Sleep() and Wakeup() solution is better than busy waiting solution for critical section problem.

## Short answer

A race condition occurs when concurrent access to shared data yields an outcome dependent on execution order. Sleep() and Wakeup() are superior to busy waiting because a blocked process does not waste CPU cycles; the scheduler deschedules it, improving efficiency for long waits. Plain sleep/wakeup still suffers lost wakeups, requiring semaphores to atomically test-and-sleep.

## Answer

#### Race condition
A **race condition** is a situation in which two or more processes read and write shared data and the final result depends on who runs exactly when. Example: in a print spooler, two processes read the same free slot `in = 7` and both write their file name in slot 7; one job is lost. Race conditions are avoided by mutual exclusion.

#### Sleep() and Wakeup() versus busy waiting
Busy-waiting solutions (Peterson, TSL, strict alternation) keep a process in a loop testing a variable until it may enter. Sleep and wakeup are system calls: `sleep()` blocks the caller (moves it to the waiting state) and `wakeup(p)` moves process *p* back to the ready state.

| Point | Busy waiting | Sleep / Wakeup |
|---|---|---|
| CPU use | Wastes CPU cycles in the loop | Blocked process uses no CPU |
| State of waiting process | Ready/running | Blocked (waiting) |
| Efficiency | Poor when wait is long | Good for long waits |
| Priority inversion | Low-priority holder may never run while high-priority process spins | Avoided, as the waiting process is blocked |
| Implementation | User-level variables | Needs kernel support |

Producer-consumer example:
```c
void producer() { while (TRUE) { item = produce();
    if (count == N) sleep();            /* buffer full */
    insert(item); count++;
    if (count == 1) wakeup(consumer); } }
void consumer() { while (TRUE) {
    if (count == 0) sleep();            /* buffer empty */
    item = remove(); count--;
    if (count == N-1) wakeup(producer);
    consume(item); } }
```
The producer sleeps when the buffer is full instead of spinning, and the consumer wakes it after removing an item. Caution: this plain version still has a race on `count` (a wakeup sent just before the other process sleeps is lost, and both may sleep forever); semaphores fix this by making the test and sleep atomic.

## Check yourself

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

1. In the print spooler example, what causes a lost print job?
   A. The printer runs out of paper
   B. Two processes read the same free slot in and both write their file name in it
   C. Sleep() is called twice
   D. The spooler directory is full
   Answer: B. Both read in = 7 before either updates it, so one overwrites the other. (Key point: Race condition)

2. Why is sleep/wakeup better than busy waiting for long waits?
   A. Sleep/wakeup needs no kernel support
   B. A blocked process uses no CPU, while a spinning one wastes cycles
   C. Sleep keeps the process in the running state
   D. Busy waiting uses less memory
   Answer: B. sleep() moves the caller to the blocked state so the CPU can run other processes. (Key point: Busy waiting vs sleep)

3. How does sleep/wakeup help with priority inversion?
   A. It raises the priority of the spinning process
   B. It prevents any process from being blocked
   C. The waiting process is blocked instead of spinning, so the low-priority holder can run and release the lock
   D. It disables interrupts
   Answer: C. With busy waiting a high-priority spinner can starve the low-priority lock holder; blocking avoids that. (Key point: Priority inversion)

4. What problem remains in the plain sleep/wakeup producer-consumer code?
   A. wakeup() blocks the caller
   B. Count cannot be changed by the consumer
   C. The buffer can never become full
   D. A wakeup sent just before the other process sleeps is lost, so both may sleep forever
   Answer: D. Test of count and sleep are not atomic; semaphores fix this by making them atomic. (Key point: Lost wakeup)

## Related in Operating System

- [Race condition; critical section approaches](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-condition-critical-section-approaches) — Operating System, chapter 3
- [Critical section problem; why execute quickly](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Acritical-section-thread-quick) — Operating System, chapter 3
- [Race condition; mutual exclusion requirements](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-mutual-exclusion-requirements) — Operating System, chapter 3
- [Race condition; optimal page replacement limits](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-condition-optimal-limitation) — Operating System, chapter 3
- [Race condition example; Peterson's algorithm](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arace-condition-example-peterson) — Operating System, chapter 3
- [Dining philosophers problem and solution](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adining-philosophers) — Operating System, chapter 3, asked 2×

## Related in other subjects

- [Preventive maintenance is better than corrective](https://www.bctengineer.com/question.md?key=semester_8%2Fconstruction_management%3Apreventive-better-than-corrective) — Construction Management, chapter 9, asked 4×
- [Why Kaiser window is better than fixed windows](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-kaiser-better) — Digital Signal Analysis and Processing, chapter 5, asked 2×
- [Closed loop better than open loop (cruise)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aclosed-loop-better-cruise-integral) — Embedded System, chapter 7
- [When wet process is better than dry](https://www.bctengineer.com/question.md?key=semester_3%2Fcivil_engineering_materials%3Awet-process-better-than-dry) — Civil Engineering Materials, chapter 5
