,
, , race condition, . , , , :
1. , . , ( load, store, test) .
2. , .
3. Pi , , . (mutual exclusion).
4. , , . , , , , remainder section, , . . (progress).
5. . , , , , . (bound waiting).
, .
:
while (some condition) {
critical section
remainder section
}
, , . , . , . .
|
|
, , , PCB.
-
. , , 0. , - 0, 1 . 0 ( 5.2.).
shared int lock = 0;
while (some condition) {
while(lock); lock = 1;
Critical section
lock = 0;
Remainder section
}
, , , , while(lock); lock = 1; . , P0 lock . , lock 1, P1. lock . .
. 0. , , . i- :
shared int turn = 0;
while (some condition) {
while(turn!= i);
Critical section
turn = 1-i;
Remainder section
}
, , : P0, P1, P0, P1, P0,... . , turn 1 P0 , , P1 remainder section.
, . .
shared int ready[2] = {0, 0};
i - , ready[i] 1. , , 0. , .
while (some condition) {
ready[i] = 1;
while(ready[1-i]);
Critical section
ready[i] = 0;
Remainder section
}
, , , , . . ready[0] = 1 0 1, ready[1] = 1. . , (deadlock).
|
|
, , (Dekker). 1981 (Peterson) . .
shared int ready[2] = {0, 0};
shared int turn;
while (some condition) {
ready[i] = 1;
turn =1- i;
while(ready[1-i] && turn == 1-i);
Critical section
ready[i] = 0;
Remainder section
}
Pi . , . . , .