---
title: "Semaphore for mutual exclusion"
subject: "Operating System"
chapter: 3
key: "semester_6/operating_system:semaphore-enforce-mutual-exclusion"
source: https://www.bctengineer.com/q/semester_6/operating_system/semaphore-enforce-mutual-exclusion
site: BCT Engineering
---

# Semaphore for mutual exclusion

Operating System · Chapter 3: Process Communication and Synchronization

- Asked in: 2075 Baisakh, 5 marks
- Video: [What is a semaphore? How do they work? (Example in C)](https://www.youtube.com/watch?v=ukM_zzrIeXs) — Jacob Sorber
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/semaphore-enforce-mutual-exclusion
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-3

## Question

How can semaphore be used to enforce mutual exclusion? Give a suitable example.

## Short answer

A binary semaphore initialized to 1 enforces mutual exclusion by placing wait(mutex) before and signal(mutex) after the critical section. The first process executing wait decrements the semaphore to 0 and enters; others block until the owner signals. For example, P1: wait(mutex); balance+=100; signal(mutex); P2: wait(mutex); balance-=50; signal(mutex);

## Answer

A **semaphore** is an integer variable with two atomic operations, `wait(S)` (down/P) and `signal(S)` (up/V). Initialising a **binary semaphore** `mutex = 1` and surrounding every critical section with wait and signal enforces mutual exclusion.

```text
wait(S):   while (S <= 0) ; S--      (or block if S == 0)
signal(S): S++                       (or wake a blocked process)
```

#### Method
```c
semaphore mutex = 1;          /* shared, initial value 1 */

Process Pi:
  do {
      wait(mutex);            /* entry section */
      /* critical section */
      signal(mutex);          /* exit section */
      /* remainder section */
  } while (TRUE);
```

#### Why it works
- mutex = 1 means the critical section is free. The first process executing `wait` makes it 0 and enters.
- Any other process executing `wait` finds 0 and blocks (it is queued, not spinning).
- When the owner executes `signal`, one blocked process is woken and enters; if none waits, mutex returns to 1.
- So at most one process is in the critical section at a time.

#### Example
Two processes P1 and P2 update a shared bank balance:
```text
P1: wait(mutex); balance = balance + 100; signal(mutex);
P2: wait(mutex); balance = balance - 50;  signal(mutex);
```
If P1 enters first (mutex becomes 0), P2 blocks at `wait` until P1 signals, so the updates occur one after the other and the final balance is always correct.

## Check yourself

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

1. What initial value of mutex enforces mutual exclusion?
   A. -1
   B. 0
   C. 1
   D. 2
   Answer: C. mutex = 1 means the critical section is free for exactly one process. (Key point: Binary semaphore initial value)

2. How is a critical section wrapped using the semaphore?
   A. wait(mutex) before and signal(mutex) after
   B. signal(mutex) at both ends
   C. wait(mutex) only after
   D. signal(mutex) before and wait(mutex) after
   Answer: A. wait is the entry section and signal is the exit section. (Key point: Entry and exit)

3. If P1 is inside the critical section (mutex = 0) and P2 executes wait, what happens?
   A. P2 enters too
   B. P2 blocks in the semaphore queue without spinning
   C. P2 sets mutex to 1
   D. P2 is terminated
   Answer: B. wait finds 0, so P2 is queued until P1 signals. (Key point: Blocked process)

4. What does signal(mutex) do when the owner leaves?
   A. Terminates all waiting processes
   B. Wakes one blocked process, or returns mutex to 1 if none waits
   C. Always sets mutex to 0
   D. Wakes all waiting processes at once
   Answer: B. One waiter enters; otherwise mutex goes back to 1 so the section is free. (Key point: Signal behaviour)

## Related in Operating System

- [Critical section; mutual exclusion](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Acritical-section-mutual-exclusion-pc) — 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
- [Producer-consumer using semaphore](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-mutual-exclusion) — Operating System, chapter 3, asked 2×
- [Producer-consumer: semaphore and messages](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apc-semaphore-message-passing) — Operating System, chapter 3
- [pipe(); semaphore in distributed systems](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Apipe-semaphore-distributed) — Operating System, chapter 3
- [TSL; semaphore operations as class](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Atsl-semaphore-class) — Operating System, chapter 3

## Related in other subjects

- [Token ring mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Atoken-ring-mutex) — Distributed System, chapter 6, asked 3×
- [Ricart-Agrawala mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aricart-agrawala) — Distributed System, chapter 6, asked 2×
- [Lamport algorithm for mutual exclusion](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alamport-mutex) — Distributed System, chapter 6
- [Mutual exclusion via sleep and wake](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Amutual-exclusion-sleep-wake) — Embedded System, chapter 6
