0% found this document useful (0 votes)
5 views3 pages

3 Process Synchronization

Process synchronization is a mechanism in operating systems that prevents race conditions, data inconsistency, and deadlocks when multiple processes access shared resources. Key concepts include critical sections, race conditions, mutual exclusion, deadlocks, starvation, semaphores, and mutexes, with common problems like the Producer-Consumer, Readers-Writers, and Dining Philosophers problems illustrating the need for synchronization. Solutions often involve using semaphores and algorithms to manage resource access and ensure proper execution order among processes.

Uploaded by

Prateek sbl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

3 Process Synchronization

Process synchronization is a mechanism in operating systems that prevents race conditions, data inconsistency, and deadlocks when multiple processes access shared resources. Key concepts include critical sections, race conditions, mutual exclusion, deadlocks, starvation, semaphores, and mutexes, with common problems like the Producer-Consumer, Readers-Writers, and Dining Philosophers problems illustrating the need for synchronization. Solutions often involve using semaphores and algorithms to manage resource access and ensure proper execution order among processes.

Uploaded by

Prateek sbl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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++;
}

7. Mutex (Mutual Exclusion Lock)


A mutex is a binary semaphore (0 or 1) used for locking a critical section. Only one process can
acquire the lock at a time. Example: Used in multi-threaded programming to prevent simultaneous
modification of shared data.

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

2. Dining Philosophers Problem:


The Dining Philosophers Problem is a classic synchronization problem that deals with multiple
processes (philosophers) competing for limited resources (chopsticks). The goal is to ensure that no
philosopher starves or deadlocks while trying to eat.
Algorithm
1. while (true) {
2. Think(); // Philosopher is thinking 🤔
3. wait(mutex); // Ensure limited philosophers can enter critical section
4. wait(chopstick[i]); // Pick up left chopstick
5. wait(chopstick[(i + 1) % 5]); // Pick up right chopstick
6.
7. Eat(); // Philosopher eats 🍜
8.
9. signal(chopstick[i]); // Put down left chopstick
10. signal(chopstick[(i + 1) % 5]); // Put down right chopstick
11. signal(mutex); // Allow another philosopher to enter critical section
12. }

You might also like