Ch. 21 · Operating Systems

Mutexes Versus Semaphores and Resource Ownership

Mutexes Versus Semaphores and Resource Ownership. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readintermediateupdated Oct 3, 2026

A mutex protects exclusive ownership; a semaphore represents available permits. Choose the primitive from the invariant rather than naming every lock equivalent.

Before you start

You should understand processes, threads, memory and basic file operations. Track ownership and state changes over time. For concurrent scenarios, write down at least two possible execution orders; the same instructions can behave differently when scheduling or resource availability changes.

The practical goal is to reason through this situation: A mutex guards one shared structure; a semaphore limits five concurrent operations. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Define exclusive state

A mutex guards a shared invariant under one ownership discipline.

Step 2: Define capacity permits

A semaphore can limit simultaneous operations to five.

Step 3: Release in every path

Errors and cancellation must not permanently consume permits.

Worked scenario

A mutex guards one shared structure; a semaphore limits five concurrent operations.

Five upload workers hold permits while a sixth waits or is rejected. A permit limits capacity but does not prevent the five workers from racing over a separate shared counter. That counter needs its own appropriate atomic or locking rule; capacity control and invariant protection solve different problems.

Common mistake

A semaphore permit does not automatically protect arbitrary compound state updates.

Verify the behavior

Inject failures and verify permit count and shared-state correctness.

Interview exercise

Limit expensive requests safely.

Answer and reasoning

Acquire a permit before work and release it in all completion paths, using separate synchronization for shared invariants.

Continue learning

Compare the scenario with the Operating Systems interview questions and test your understanding with the Operating Systems MCQs. For terminology and implementation details, consult the reference material.

More in Operating Systems

read ✓Operating Systems · hard

Operating Systems: CPU Cache Locality

Improve performance with spatial and temporal locality, avoid pointer chasing, and understand false sharing between threads.

~2 min readread →
read ✓Operating Systems · hard

Copy-on-Write Memory and fork

How copy-on-write lets fork share pages until a write, why RSS can be misleading, and the implications for memory and latency.

~2 min readread →
read ✓Operating Systems · hard

Operating Systems: epoll and select

How select, poll and epoll report I/O readiness, and the difference between level- and edge-triggered notifications.

~2 min readread →
esc