Process Synchronization
Presented By
Mr. Ajay Maurya
Process Synchrnization
• Process Synchrnization is a technique used in an operating
system to Control the execution of multiple processes so that
they can safely shared data and resources without causing
errors.
• Shared Resources (Variable, Memory, Code, Resources(CPU,
Printer, Scanner.))
• It is mainly required in Concurrent(Co-operative)
Concurrent(Co Processes.
Why Synchronization Needed?
• When Two or More Processes access shared data at the same time, It
may cause:
• Data Inconsistency
• Race Condition
• Unexpected Result
Example
Process P1 Process P2
Int shared = 5
Int x = shared Int y = shared
x++ Y--
sleep(1) sleep(1)
shared = x shared = y
Result
• Final output will be based on execution order:
• If firstly Process P1 is running then output will be 4.
• If firstly Process P2 is running then output wiil be 6.
• This condition in known as Race Condition. Processes are doing race
in between to get the results based on execution order.
Code Segment(Problem)
Code Segment(Solution)
Problem in Concurrency
Race Condition : When two processes access shared data at
the same time and results depends on execution order.
Deadlock : Two or more processes are waiting for each other
to release resources.
Starvation : A process never CPU Time.
Critical Section : A critical section is the part of program
where a process accesses shared resource or shared data.
• Only one process at a time should execute the Critical Section to
avoid errors or data inconsistency.
inconsistency
Why Critical Section Needed?
• When multiple processes are running concurrently
and access shared resources, following problem may
occure:
• Race Condition
• Data Inconsistency
• Unexpected Result
• Therefore, the Operating System must control access
to the Critical Section.
Solutions/Requirement for Critical Section
A correct solution must satisfy three condition:
1. Mutual Exclusion
• Only one process can execute the critical section at a time.
2. Progress
• If no process in Critical Section, another process should be
allowed to enter.
3. Bound Waiting
• A process should not wait forever to enter critical section.
Critical Section
Structure of Critical Section Example of Critical Section
A typical process using a do{
critical section has 4-Parts Entry Section
1. Entry Section Critical Section
2. Critical Section
Exit Section
3. Exiti Section
Remainder Section
4. Remainder Section
}while(true)
Method to solve Critical Section Problem
Operating System use several Techniques:
• Lock Variable
• Peterson’s Solutions
• Semaphores
• Mutex
• Monitors
Producer Consumer Problem
• The Producer Consumer Problem is a classical Process
Synchronization problem in an operating system.
• It occurs when:
• A Producer process produces data/items.
• A Consumer process consumes those data/items.
• Both are shared a common buffer(Shared Memory)
• The buffer has limited size, so proper coordination is
required.
Main issue in Producer Consumer Problem
Without synchronization, the following problems may occur :
• Buffer Overflow
• Producer tries to add an item when the buffer is full.
• Buffer Underflow
• Consumer tries to remove an item when the buffer is
empty.
• Race Condition
• Producer and Consumer access the buffer simultaneously,
causing incorrect data.
Pseudo Code for Producer Consumer Problem
Pseudo Code for Printer Spooler Problem
Spooler Directory
0