The Need for Process Synchronization
Process synchronization is the coordination of processes or threads that access and modify shared data at the same time, so the result depends on program logic rather than unpredictable timing. Without it, even counter = counter + 1, which is really a separate read, add and write, can lose an update when two threads interleave.
When only one process at a time touches a piece of shared data, there's no ambiguity about the result. The moment two or more processes or threads can read and write that same data concurrently, the final outcome can depend on the exact, unpredictable timing of which one happens to run first, and that's precisely the problem process synchronization exists to solve.
Why concurrent access is genuinely dangerous
Consider two threads both trying to increment a shared counter variable by executing counter = counter + 1. This single line of code is actually not one atomic step at the hardware level, it's really: read the current value of counter, add 1 to it, write the new value back. If both threads read the value at nearly the same moment, both see the same starting value, both compute the same "current value plus one," and both write back the same result, and the counter only increases by 1 total, even though two increments were supposed to happen. This exact scenario is called a race condition, and it's covered in more depth in its own dedicated article.
What synchronization is actually for
Process synchronization is the set of techniques and mechanisms (covered across the following articles: the critical section problem, Peterson's solution, semaphores) that guarantee shared data is only ever accessed by one process or thread at a time when a conflicting operation is happening, so the final result is always correct and predictable, regardless of the exact timing the OS happens to schedule things in.
A concrete example
An airline booking system with two customers simultaneously trying to book the very last available seat is a textbook synchronization problem. Without synchronization, both processes could read "1 seat remaining," both decide there's a seat available, both proceed to book it, and both write back "0 seats remaining," resulting in two customers holding a ticket for one physical seat. With proper synchronization, whichever process gets there first is guaranteed exclusive access to check and update the seat count, so the second process, arriving even a fraction of a second later, correctly sees the already-updated "0 seats remaining" and is told the seat is gone.
Where this matters in real systems
Every real database enforces synchronization on shared rows precisely to avoid exactly this kind of double-booking or lost-update problem, transaction isolation and locking mechanisms (covered in the DBMS subject's Transaction Processing topics) are a direct real-world application of process synchronization principles. Multithreaded applications, a web server handling many simultaneous requests that touch shared in-memory data, need the exact same discipline, and getting it wrong produces bugs that are notoriously hard to reproduce, since they only show up under specific, rare timing conditions.
What interviewers actually ask about process synchronization
- "Why is
counter++not safe to run from multiple threads without synchronization?" Because it's really three separate steps (read, add, write) at the hardware level, and two threads interleaving those steps can both read the same starting value and end up overwriting each other's update, losing an increment. - "What is process synchronization trying to prevent?" Incorrect, timing-dependent results caused by multiple processes or threads accessing and modifying shared data concurrently.
- "Give a real-world example of why this matters." Two customers simultaneously booking the last seat on a flight, without synchronization both could succeed and double-book the same seat.
- "Where in real systems is this problem actually solved in practice?" Database transaction isolation and locking, and multithreaded application code using synchronization primitives like locks or semaphores around shared data.
Key takeaways
- Concurrent, unsynchronized access to shared data can produce incorrect results that depend on unpredictable timing.
- The classic example is a race condition, where an operation that looks like one step is really several, and interleaving those steps across threads loses updates.
- Process synchronization is the set of techniques that guarantee correct, predictable results regardless of scheduling timing.
- Database transaction isolation is a direct real-world application of these same synchronization principles.
More in Process Synchronization
Browse all Operating Systems topics
Log in to save which topics you've read across every subject.
Frequently asked questions
Why is `counter++` not safe to run from multiple threads without synchronization?
Because it's really three separate steps (read, add, write) at the hardware level, and two threads interleaving those steps can both read the same starting value and end up overwriting each other's update, losing an increment.
What is process synchronization trying to prevent?
Incorrect, timing-dependent results caused by multiple processes or threads accessing and modifying shared data concurrently.
Give a real-world example of why this matters.
Two customers simultaneously booking the last seat on a flight, without synchronization both could succeed and double-book the same seat.
Where in real systems is this problem actually solved in practice?
Database transaction isolation and locking, and multithreaded application code using synchronization primitives like locks or semaphores around shared data.