Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

 In Multi Programming system where multiple process are

executed concurrently, And sharing system resources by


processors 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. Some of the
problems are discussed below.
 What problem will occur when multiple process share
same resources:

Here P have a 3 instruction.


P1,P2 = Process
a=10
 For example

Here-
a is a integer variable=10
P have a 3 instruction(Read, operation, Write).

 Suppose P1,P2 are 2 Process which share this variable in systematic order
then output will be:-
 After execution-
P1=11
P2=12
 Now, if we try use this shared resources in different order then output
will be-
Process P2
Process P1 •a=10
•a=10 •a=a+1 //11
•Context switch to P2 •Write(11)
•Context switch to P1
Charge taken from P2
a=a+1 //12
Write(12)

This condition is called Race condition.


Race Condition: when order of execution can change result so that called Race
condition. To stay away of this problem process must be synchronized.
Context switching: when Kernel transfer the control of CPU from one executing process
to another that is ready to run.
 So in order to avoid these type of inconsistency of data the process
should be synchronized with each other.

 Process Synchronization was introduced to handle the problem that arise


when multiple process execute. one of the problem is-

 Critical Section Problem- An area where we access our share resources is


called critical section. In other word an area in process execution where
hardware and software are access the sharable resources is called critical
section.

Definition : Critical section is a group of instructions and region of code


that need to be executed atomically or accessing a resources. It can’t be
accessed by more than one process.
 Criteria for making solutions of critical region:
 It means any solution will be judge on the basis of these 3 criteria.

 Mutual Exclusion: if the process P is execute in critical section, then


no other process can be executing in their critical section. This is a
mandatory criteria.

 Progress: in this criteria we consider only those process which actually


wants enter into a critical section. It is also a mandatory criteria.

 Bounded Wait: this suggest that there must be a upper bound to which a
process wait. In other words after specific number of terms the process
will be bound or enter in critical section.
But this criteria is not always fulfill so this is optional criteria.
Case 1:
 Using Turn variable Two process solution for Critical section problem:

Generalized form of any Process


Here P0 , P1 = Process
Both Process are
independent.
Suppose, start with P0
Initial value of turn= 0

Working:
 P0 enter in while (turn!=0) condition false. Then loop will terminated. And enter in
critical section.
 Now for checking that this code block fulfill the 3 criteria, so we suppose that here
Context Switching is done and charge will taken by P1.
 In P1, while condition will check where turn!=1 and condition true but there are no
body of while loop so loop will execute until condition will not be a false.
 Later, again P0 will take a charge and execute code block where Turn = 1. and P0 will
execute.
 Now again we check that now P1 will be able to enter in Critical section or not:
 Now turn = 1 and while(turn!=1); condition will false. And P1 will enter in Critical
section.
 Here we can say that this code fulfill the 1st criteria that is Mutual Exclusion. And P0
and P1 execute in Round Robin fashion.
Case 2:
 Now let’s try to second criteria that is Progress. But In the scenario that we have
discussed not fulfill the Progress criteria because the rule of progress is that the
process should be choose if it actually wants to go in the critical section. And this code
never asked the process to take a charge or not.
 Let’s Discuss using Flag Variable:

Initially
Flag

 Start with process P0=0 and Flag[0]=T so –


 Here we use both Turn variable and Flag array.
 Semaphore can do any work in a process
means it can decide the order of execution
among the process. But if we use Semaphore
to solve Critical Section problem we initialize
it with 1.
 Application of Semaphore:
 Critical Section problem
 Order of Execution between the process
 Resource management

We discuss the semaphore use in critical section.


 We always initialize it with 1. when we initialize it 1 then we cannot access
directly. So we accessed only through two standard atomic operations:
I. Wait() II. Signal()

Definition: Semaphore is an Integer variable S, that apart


from initialization is accessed only through two standard
atomic operation.

You might also like