What is Process Synchronization?
Semaphores, mutexes, and monitors, told as a story about the rule for handing out the lift key.
Two mechanics reach for the same lift at the same time.Neither knows the other is reaching for it.
The crash happens in the gap between checking and using.A crash in that gap is called a race condition.
Synchronization = only the key-holder may use the lift.The next person waits until the key comes back.
Each tool differs in how many get in at once.A mutex resembles a semaphore with N=1, but only its locker can unlock it.
If the key isn't returned, the next mechanic freezes in place.Holding one key while waiting for another can lead to deadlock.
Process synchronization = the rule that lets several tasks touch a shared resource like the lift only one at a time (up to N with a semaphore).
When several processes or threads touch the same resource (a variable, file, or memory region) at once, a race condition results. A mutex enforces mutual exclusion so only one enters the critical section at a time; a semaphore uses a counter to allow up to N; a monitor bundles a lock with condition variables at the language level to handle waiting and signaling safely.
When grown-ups say it
- Race Condition
- A crash in the gap between checking and using. Scramble the order, and the result changes each time.
- Mutex
- A lock with exactly one key. Only one task gets in at a time.
- Semaphore
- A lock with N keys. Up to N can be inside at once.
- Monitor
- A room with a built-in waiting lounge. Bundles a lock with condition variables.
- Deadlock
- Frozen forever, each waiting on the other's key. Holding a key while waiting on each other's leads straight here. → the garage frozen waiting on each other's keys
- Process/Thread
- Several hands working the same car. Synchronization matters most for threads, which share "the same car." → one car, many hands
- CPU Scheduling
- The car order written on the job board. "Who goes first" is scheduling — synchronization is "don't touch at once." → who gets the lift first
- Cache Memory
- A small toolbox holding only the frequently used tools. Similar coordination is needed when several cores share a cache line. → the toolbox kept right beside you