What is process synchronization?
Answer
Process synchronization is the coordination of concurrent processes/threads that share data to prevent race conditions and ensure data consistency. It manages the order of access and execution of concurrent operations. Why synchronization is needed: multiple processes share data (memory, files, I/O). Without coordination, concurrent access leads to inconsistent results (race conditions). Example: bank account balance updated by two concurrent transactions — both read 1000, both add 100, both write 1100 → result should be 1200 but is 1100. Synchronization mechanisms: (1) Mutex locks: ensure only one thread in critical section; (2) Semaphores: integer-based signaling, counting access; (3) Monitors: high-level synchronization construct — encapsulates data and operations, ensures only one process active at a time within; (4) Condition variables: allow threads to wait within a monitor until a condition becomes true — wait(), notify(), notifyAll() in Java; (5) Atomic operations: indivisible operations that complete without interruption (CAS — Compare and Swap, atomic fetch-and-add); (6) Lock-free data structures: use atomic operations to avoid locks altogether; (7) Message passing: avoid shared state entirely — communicate via messages. Synchronization in Java: synchronized keyword (method/block), wait()/notify(), java.util.concurrent (ReentrantLock, CountDownLatch, CyclicBarrier, Semaphore, BlockingQueue). Goal: ensure safe access to shared resources while maximizing concurrency — too much synchronization causes bottlenecks; too little causes races.
Previous
What is the difference between multiprogramming, multitasking, and multiprocessing?
Next
What is a monitor in operating systems?