int turn;
int interested[2]; //all
values initially 0
void enter_region(int process)
{
int other;
other = 1 - process;
// position 1
interested[process]=1;
// position 2
turn = process;
// position 3
while(turn == process &&
interested[other] == 1);
}
void leave_region(int process)
{
interested[process] = 0;
}
The following explain how it work when interrupt at position 1:
Key: turn
and interested[]are
global
variables.
other
is a local variable.
|
|
|
|
|
|
|
|
|
|
|
|
|
then process 1 enter CS(critical section) |
|
|
|
|
|
|
|
|
then busy wait and give CPU back to process 1 |
|
|
after that interested[1]=0 and give CPU to process 0 |
|
|
then process 0 enter CS(critical section) |
The following explain how it work when interrupt at position 2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
then process 1 wait and give CPU back to process 0 |
|
|
|
|
|
then busy wait and give CPU back to process 1 |
|
|
then process 1 enter CS(critical section) |
|
|
|
|
|
then busy wait and give CPU back to process 1 |
|
|
after that interested[1]=0 and give CPU to process 0 |
|
|
then process 0 enter CS(critical section) |
The following explain how it work when interrupt at position 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
then process 1 wait and give CPU back to process 0 |
|
|
then process 0 enter CS(critical section) |
|
|
|
|
|
then process 1 wait and give CPU back to process 0 |
|
|
after that interested[0]=0 and give CPU to process 1 |
|
|
then process 1 enter CS(critical section) |
Note: