Process Synchronization in an Operating System (OS) is a concept that ensures that
multiple processes can operate correctly and share resources (like memory, files, CPU, etc.)
without conflict.
🔑 Why is Process Synchronization Needed?
When multiple processes access shared resources, such as variables or files, we
must avoid race conditions, where the output depends on the timing of the processes.
Synchronization ensures data consistency, integrity, and correctness.
Conditions for Correct Synchronization
To solve the critical section problem, a synchronization solution must
satisfy:
Mutual Exclusion: Only one process can be in the critical section at a
time.
Progress: If no process is in the critical section, any process that wants
to enter should be allowed to do so without unnecessary delay.
Bounded Waiting: There should be a limit on how many times other
processes can enter their critical sections before a waiting process is
allowed entr
1. Critical Section
A critical section is a portion of code that accesses shared resources.
Only one process should execute in the critical section at a time to prevent data
corruption.
2. Race Condition
Occurs when multiple processes modify shared data simultaneously, and the result
depends on the timing of context switches.
3. Mutual Exclusion
Ensures that only one process can enter the critical section at a time.
4. Progress and Bounded Waiting
Progress: If no process is in the critical section, one waiting process should be
allowed to enter.
Bounded Waiting: A process should not wait indefinitely to enter the critical section.
3. Semaphores
Binary Semaphore (Mutex): Allows one process at a time.
Counting Semaphore: Allows up to N processes simultaneously.
Operated using wait() and signal() functions.
A mutex (short for Mutual Exclusion) is a synchronization primitive used in an operating system to
allow only one process or thread to access a critical section or shared resource at a time. It prevents
race conditions and ensures data consistency.
5. Monitors
High-level synchronization construct (used in high-level languages like Java).
Only one process may be active in the monitor at a time.
6. Condition Variables
Used with monitors for signaling between threads or processes.
What Causes a Race Condition?
Multiple processes or threads access shared resources (e.g., memory, files)
concurrently.
At least one of them modifies the resource.
There's no proper synchronization mechanism in place (like mutexes, semaphores,
etc.).
How to Prevent Race Conditions:
1. Mutex (Mutual Exclusion Object):
Ensures that only one thread can access a resource at a time.
2. Semaphores:
Used to control access when multiple threads need a limited number of resources.
3. Monitors and Locks:
High-level constructs in many programming languages (e.g., synchronized in Java).
4. Atomic Operations:
Operations that are completed in a single CPU instruction, ensuring no other thread
can see the operation half-complete.
wait(S):
while S <= 0:
; // Busy wait or block
S = S - 1;
signal() Operation
signal(S):
S = S + 1;
Increments the value of the semaphore.