Professional Documents
Culture Documents
Critical Section and Semaphores
Critical Section and Semaphores
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)
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:
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