Understanding Critical
Section Problems and
Solutions with Semaphores
Group Members :
Ahtisham Arif (048)
Hifza Iftikhar (039)
Aiman Tariq (011)
Manahil Rehman(034)
01.
Introduction to
Process
Synchronization
Process Synchronization
Definition:
Process synchronization refers to the coordination
of concurrent processes to ensure that they do
not interfere with each other when accessing
shared resources.
Importance:
Synchronization is crucial to maintain data
consistency and prevent race conditions,
deadlocks, and starvation.
Critical Section
Problems 1/2
Definition:
A critical section is a segment of code where shared
resources are accessed.
Issues Arising in Process Synchronization:
Race Conditions: Occur when multiple processes
access and manipulate shared data concurrently,
leading to unpredictable results.
Critical Section
Problems 2/2
Deadlocks: Occur when two or more
processes are waiting indefinitely for resources
held by each other.
Starvation: Occurs when a process is
perpetually denied necessary resources to
proceed with its execution.
Problem 1 –
The Producer-Consumer Problem 1/2
Producer-Consumer Problem:
A classic synchronization issue involving multiple processes.
Producer's Role:
• Generates items.
• Places items into a shared, fixed-size memory buffer.
• Repeats the process.
Consumer's Role:
• Retrieves items from the shared buffer.
• Consumes the retrieved items.
Problem 1 –
The Producer-Consumer Problem 2/2
Shared Buffer:
A fixed-size memory space used by
both Producer and Consumer.
Goal:
Ensure efficient and conflict-free
operation between Producer and
Consumer processes.
Example of Producer consumer
Problem algorithm
Problem 2 –
The reader writer problem 1/2
Reader-Writer Problem:
The Reader-Writer Problem is a classic synchronization issue in
operating systems (OS) and concurrent programming. It revolves
around the challenge of managing shared resources, specifically a
data structure or a section of code, that is accessed by multiple
threads.
Problem 2 –
The reader writer problem 1/2
Problem parameters:
• One set of data is shared among a number of processes
• Once a writer is ready, it performs its write. Only one writer may write at
a time
• If a process is writing, no other process can read it
• If at least one reader is reading, no other process can write
• Readers may not write and only read
Problem 3 –
The dining Philosophers problem
The Dining Philosophers
problem is a classic
synchronization and concurrency
problem that illustrates the
challenges of resource sharing
and potential deadlocks.
Problem 3 –
The Dining Philosophers
Deadlock: If all philosophers pick up
problem(Example)
one fork simultaneously and wait for the
other, a deadlock can occur. The use of the
lock semaphore aims to mitigate this by
ensuring that only one philosopher can
attempt to pick up both forks at a time.
03.
Semaphores and
Solution to all
problems
Semaphores 1/2
1. Definition:
Semaphores are synchronization tools used to control
access to shared resources by multiple processes in a
concurrent system.
2. Types of Semaphores:
• Binary Semaphore (Mutex): Can have only two
values (0 or 1), used for mutual exclusion.
• Counting Semaphore: Can have a range of values,
used for resource counting.
Semaphores Solve
Synchronization Problems
• Wait (P) Operation: Decreases the semaphore
value, potentially blocking the process if the
value becomes negative.
• Signal (V) Operation: Increases the
semaphore value, waking up a blocked process
if any.
03(a)
.
Semaphores in
Producer
consumer problem
How Semaphores avoid deadlocks:
• Mutual Exclusion
• Preventing Buffer Overflow and
Underflow Producer
• Avoiding Deadlock
• Fair Resource Allocation
• Efficient Synchronization
Consumer
03(b)
.
Semaphores in
Reader Writer
problem
Semaphore implementation in Reader writer
Sequence Diagram to explain the concept
Semaphores in Dining Philosopher
• Binary Semaphores for Mutual Exclusion:
Use binary semaphores for each fork to ensure that no
two philosophers pick up the same fork at the same time.
• Mutex to Prevent Deadlock:
Use a mutex to control access to the critical section where
a philosopher attempts to pick up both forks.
Semaphores Implementation in Dining Philosopher
Sequence Diagram to explain the concept
• The diagram illustrates the interaction
between a philosopher and the
semaphores (mutex, left fork, and right
fork) to solve the dining philosopher
problem.
• The use of wait and signal ensures
mutual exclusion and proper
synchronization, preventing deadlocks
and ensuring that the philosopher can
pick up both forks only when both are
available.
• The conditional check (alt) is crucial for
checking fork availability and deciding the
next steps.
04.
Semaphores
Mechanism
Sequence Diagram to explain the concept
Semaphore Initialization:
Starts with a count representing available resources.
P (Wait) Operation:
• A process requests a resource.
• If the semaphore count is greater than 0, the count
is decremented, and the process acquires the
resource.
• If the count is 0 or less, the process is placed in a
waiting queue.
V (Signal) Operation:
• A process releases a resource.
• The semaphore count is incremented.
• If there are processes in the waiting queue, one is
unblocked and acquires the resource.
05.
Advantages and
Disadvantages of
Semaphores
Advantages of Semaphores
•Simple Implementation
•Effective in Ensuring Mutual Exclusion
•Resource Protection
•Predictable Behavior
•Synchronization Control
•Improved Reliability
Disadvantages of Semaphores
• Potential for Deadlocks if Not Used Carefully
• More Complex Management in Large
Systems
• Reduced Parallelism
• Increased Overhead
• Priority Inversion
• Complex Error Handling
• Scalability Issues
• Resource Contention
06.
Conclusion
Conclusion
In conclusion, process synchronization ensures data
consistency and prevents system crashes. Semaphores
are essential in managing access to shared resources
but must be used carefully to avoid issues like
deadlocks. Proper synchronization is key to maintaining
system stability and performance. Thank you.
Thank you!!
For your attention
Any
Questions