Process synchronization allows multiple processes to access shared resources in a coordinated way to avoid inconsistencies. It introduces mechanisms to handle issues that arise from concurrent process execution, such as ensuring only one process accesses a printer at a time to prevent deadlocks. Common techniques for process synchronization include using a critical section to restrict access to shared resources to only one process at a time, Peterson's algorithm for mutual exclusion between two processes, and semaphores to coordinate access through wait and signal operations.
2. Process Synchronization
• Process Synchronization means sharing system resources
by processes in a such a way that, Concurrent access to shared data is
handled thereby minimizing the chance of inconsistent data.
• Process Synchronization was introduced to handle problems that arose
while multiple process executions.
• Examples of Process Synchronization , Printer are a shared resource
which multiple process can access and use but can not use it at the
same time. If they try to access at same time deadlock occurs.
4. Critical Section
• The area through process execution through which a process try to
access a shared resource is called critical section.
5. Critical Section Environment
• A Critical Section Environment contains:
• Entry Section Code requesting entry into the critical section.
• Critical Section Code in which only one process can execute at any one
time.
• Exit Section The end of the critical section, releasing or allowing others in.
• Remainder Section Rest of the code AFTER the critical section.
6. Critical Section Condition
• A process has to fulfil three condition before entering into critical
section.
1. Mutual Exclusive
2. Progress
3. Bounded wait.
In above condition first two are mandatory and a process must has to fulfil these
conditions before accessing critical section.
7. Mutual Exclusive
• Mutual Exclusion: No more than one process can execute in its critical
section at one time.
P0 P1
while(1){ while(1){
while(turn!=0); while(turn!=1);
//Critical Section. //Critical Section
turn=1; turn=0;
//Remainder Section //Remainder Section
} }
8. Progress
• If no one is in the critical section and someone wants in, then those
processes not in their remainder section must be able to decide in a finite
time who should go in.
P0 P1
while(1){ while(1){
flag[0]=T flag[1]=T
while(flag[1]); while(flag[0]);
//Critical Section. //Critical Section
flag[0]=F; flag[1]=F;
//Remainder Section //Remainder Section
} }
9. Bounded Wait
• All requesters must eventually be let into the critical section
• After a process makes a request for getting into its critical section,
there is a limit for how many other processes can get into their critical
section, before this process's request is granted. So after the limit is
reached, system must grant the process permission to get into its
critical section.
10. Peterson Solution
• Peterson's algorithm is used for mutual exclusion and allows two
processes to share a single-use resource without conflict.
• Peterson's formula originally worked only with two processes, but has
since been generalized for more than two.
• To handle the problem of Critical Section (CS), Peterson gave an
algorithm.
• Mutual exclusion, no progress two essential criteria used to solve the
critical section problem when using the algorithm.
12. Semaphore
• A semaphore is an integer variable “int s”, that apart from
introduction, is accessed only through two standard atomic
operations.
• FORMAT:
do{
wait ( S );
CRITICAL SECTION
signal( S );
REMAINDER
}while(T)
13. Semaphore
• Wait : decrement the value of its argument S as soon as it would become
non-negative.
wait(s){
while(s<=0);
s--;}
• Signal : increment the value of its argument, S as an individual operation.
signal(s){
s++;
}
14. Example
do{
wait ( S );
CRITICAL SECTION
signal( S );
REMAINDER
}while(T);
wait(s){ signal(s){
while(s<=0); s++;
s--;} }