2. Definition
Example of Critical section problem
Solution to critical section problem
Software solution
Algorithm 1
Algorithm 2
Algorithm 3
Critical Region
3. When a process is accessing shared
modifiable data or a resource that can only
operate on behalf of one process at a time ,
the process is said to be in a critical section.
When one process is in a critical section , all
other processes (at least those that access
the shared modifiable data and/or resource)
are excluded from their critical section.
4. n processes all competing to use some shared data
Each process has a code segment, called critical section, in which
the shared data is accessed.
Problem – ensure that when one process is executing in its critical
section, no other process is allowed to execute in its critical section.
5. Transfer Rs. 100 from saving account to checking account
P1 P2
Saving = saving – 100 saving = saving * 1.01
Checking = checking +100 checking = checking * 101
Initially : saving = 100
checking = 0
P1 ran first & P2 ran first & P1’s first line then P2
P2 ran second p1 ran second & P1’s second line
Saving = 0 saving = 1 saving = 0
Checking = 101 checking = 100 checking = 100
6. 1. Mutual Exclusion. If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections.
2. Progress. If no process is executing in its critical section and
there exist some processes that wish to enter their critical
section, then the selection of the processes that will enter the
critical section next cannot be postponed indefinitely.
3. Bounded Waiting. A bound must exist on the number of times
that other processes are allowed to enter their critical sections
after a process has made a request to enter its critical section
and before that request is granted.
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes.
7. Only 2 processes, P0 and P1
General structure of process Pi (other process Pj)
do {
entry section
critical section
exit section
reminder section
} while (1);
Processes may share some common variables to synchronize
their actions.
8. Shared variables:
int turn;
initially turn = 0
turn = i Pi can enter its critical section
Process Pi
do {
while (turn != i) ;
critical section
turn = j;
reminder section
} while (1);
Satisfies mutual exclusion, but not progress
9. Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion
◦ Progress
◦ Bounded wait
10. public class Algorithm_1 implements MutualExclusion {
private volatile int turn;
public Algorithm_1() {
turn = TURN_0;
}
public void enteringCriticalSection(int t) {
while(turn != t)
Thread.yield();
}
public void leavingCriticalSection(int t){
turn = 1 - t;
}
}
11. Shared variables
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [i] = true Pi ready to enter its critical section
Process Pi
do {
flag[i] := true;
while (flag[j]) ;
critical section
flag [i] = false;
remainder section
} while (1);
Satisfies mutual exclusion, but not progress
requirement.
12. Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion
◦ Progress
◦ Bounded wait
15. Combined shared variables of algorithms 1
and 2.
Process Pi
do {
flag [i]:= true;
turn = j;
while (flag [j] and turn = j) ;
critical section
flag [i] = false;
remainder section
} while (1);
Meets all three requirements; solves the
critical-section problem for two processes.
16. Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion
◦ Progress
◦ Bounded wait
17. public class Algorithm_3 implements MutualExclusion {
private volatile int turn;
private volatile boolean flag0;
private volatile boolean flag1;
public Algorithm_3() {
flag0 = false;
flag1 = false;
turn = TURN_0;
}
public void enteringCriticalSection( int t) {
int other = 1 - t;
turn = other;
if(t == 0) {
flag0 = true;
while((flag0 == true) && (turn == other))
Thread.yield();
} else {
flag1 = true;
while((flag0 == true) && (turn == other))
Thread.yield();
}
}
19. High-level synchronization construct
A shared variable v of type T, is declared
as:
v: shared T
Variable v accessed only inside statement
region v when B do S
where B is a boolean expression.
While statement S is being executed, no
other process can access variable v.
20. Regions referring to the same shared
variable exclude each other in time.
When a process tries to execute the
region statement, the Boolean
expression B is evaluated. If B is true,
statement S is executed. If it is false,
the process is delayed until B becomes
true and no other process is in the
region associated with v.