3 Process Synchronization
3 Process Synchronization
Process synchronization in an operating system is a mechanism that ensures that multiple processes or
threads can execute concurrently without leading to race conditions, data inconsistency, or deadlocks
when accessing shared resources. It is essential in multi-threading and multi-processing environments.
Key Terms Related to Process Synchronization:
1. Critical Section
A critical section is a part of the program where a process accesses shared resources (e.g., shared
variables, memory, files). If multiple processes enter the critical section at the same time, it may lead
to inconsistencies.
Solution: Ensure only one process enters the critical section at a time.
2. Race Condition
A race condition occurs when two or more processes try to read/write a shared resource
simultaneously, leading to unpredictable behavior. Example: Two threads update a shared counter
simultaneously without synchronization.
3. Mutual Exclusion
Ensures that only one process can enter the critical section at a time. Example: Using locks,
semaphores, or monitors to prevent multiple processes from executing critical code simultaneously.
4. Deadlock
Deadlock occurs when two or more processes are stuck in a circular wait, each waiting for a resource
held by another. Example: Process A holds Resource 1 and waits for Resource 2, while Process B
holds Resource 2 and waits for Resource 1.
Solution: Deadlock prevention, detection, and avoidance strategies.
5. Starvation
Starvation happens when a process waits indefinitely because higher-priority processes keep
executing before it. Example: In a scheduling system, if high-priority tasks keep arriving, a low-
priority process may never get CPU time.
Solution: Implementing fair scheduling algorithms like Round Robin.
6. Semaphore
A semaphore is a synchronization primitive used to control access to shared resources.
It is an integer variable that can be:
Binary (0 or 1) → Acts like a mutex (lock).
Counting (N resources available) → Used when multiple instances of a resource exist.
Operations:
Wait(S): Decrease the semaphore (block if S <= 0).
Signal(S): Increase the semaphore (release resource).
Definition of the wait () operation
wait (S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal () operation
signal (S) {
S++;
}
Problems:
1. Producer-Consumer Problem:
The Producer-Consumer Problem is a classic synchronization problem where:
A Producer generates data and adds it to a shared buffer.
A Consumer removes data from the shared buffer and processes it.
The buffer has a fixed size (bounded buffer),
The Producer must wait if the buffer is full.
The Consumer must wait if the buffer is empty.
Solution: Using Semaphores
To solve this problem, we use three semaphores:
empty → Counts available empty slots in the buffer.
full → Counts the filled slots in the buffer.
mutex → Ensures mutual exclusion when accessing the buffer.
Algorithm for Producer
1. while (true) {
2. ProduceItem(); // Produce an item
3. wait(empty); // Decrease empty slot count (Wait if buffer is full)
4. wait(mutex); // Lock buffer access (Critical Section)
5.
6. Add item to BUFFER; // Place item in buffer
7.
8. signal(mutex); // Unlock buffer access
9. signal(full); // Increase full slot count
10. }
Algorithm for Producer
1. while (true) {
2. wait(full); // Decrease full slot count (Wait if buffer is empty)
3. wait(mutex); // Lock buffer access (Critical Section)
4.
5. Remove item from BUFFER; // Take item from buffer
6.
7. signal(mutex); // Unlock buffer access
8. signal(empty); // Increase empty slot count
9.
10. ConsumeItem(); // Process the item
11. }
2. Readers-Writers Problem:
The Readers-Writers Problem is a classical process synchronization problem that arises when multiple
processes (readers and writers) try to access a shared resource (e.g., a file or database).
Reader → Reads the shared resource (does not modify it).
Writer → Writes to the shared resource (modifies it).
Synchronization Issue →
Multiple readers can read simultaneously. ✅
Writers need exclusive access (no other readers or writers should be active). ❌
If not handled properly, it can lead to race conditions, starvation, or data inconsistency.
Algorithm for Readers
1. wait(mutex); // Lock read count update
2. readCount++;
3. if (readCount == 1)
4. wait(writeLock); // First reader locks writing
5. signal(mutex); // Unlock read count update
6. Read from resource; // Perform reading
7. wait(mutex); // Lock read count update
8. readCount--;
9. if (readCount == 0)
10. signal(writeLock); // Last reader unlocks writing
11. signal(mutex); // Unlock read count update
Algorithm for Writers
1. wait (writeLock); // Ensure exclusive write access
2. Write to resource; // Perform writing
3. signal (writeLock); // Release write lock