Sie sind auf Seite 1von 2

7.2 Prove that, in the bakery algorithm (Section 7.2.

2), the following property holds: If


Pi is in its critical section and Pk (k != i) has already chosen its number[k] !
=0,then(number[i],i) < (number[k],k).
Answer:
Suppose that Pi is in its critical section, and Pk(k!=i) has already chosen its number[k], there
are two cases:
1. Pk has already chosen its number when Pi does the last test before entering its critical
section.
In this case, if (number([i], i) < (number[k],k) does not hold, since they can not be equal, so
(number[i],i) > (number[k],k). Suppose this is true, then P i can not get into the cirtical section
before Pk does, and Pi will looping at the last while statement until the condition does not
hold, which is modified by Pk when it exits from its critical section.Note that if Pk get a new
number again, it must be larger than that of P i's.
2. Pk did not chose its number when Pi does the last test before entering its critical section.
In this case, since Pk has chosen its number when Pi is in its critical section, Pk must chose its
number later than Pi. According to the algorithm, P k can only get a bigger number than Pi, so
(number[i],i) < number([k],k) holds.

6.3 The first known correct software solution to the critical-section problem for two
processes was developed by Dekker. The two processes, P0 and P1, share the following
variables:
boolean flag[2]; /* initially false */
int turn;
The structure of process Pi (i = 0 or 1), with Pj (j = 1 or 0) being the other process, is
shown in Figure 7.27.
Prove that the algorithm satisfies all three requirements for the critical-section problem.

do{
flag[i]=true;
while(flag[j]){
if(turn==j){
flag[i]=false;
while(turn==j);
flag[i]=true;
}
}
Critical section
turn = j;
flag[i]=false
Remainder section
}while(1);
Figure 7.27 The structure of process Pi in Dekker's algorithm.

Answer: To prove property (a) we note that each Pi enters its critical section only if flag[j] =
false. Since only Pi can update flag[j], and since Pi inspects flag[j] only while flag[i]=true,
the result follows.
a. To prove property (b), we first note that the value of the variable turn is changed only at
the end of the critical section. Suppose that only process Pi wants to enter the critical section.
In this case, it will find flag[j]=false and immediately enter the critical section, independent
of the current value of turn. Suppose that both processes want to enter the critical section, and
the value of turn = 0. Two cases are now possible. If Pi finds flag[0] = false then it will enter
the critical section. If Pi finds flag[0] = true then we claim that P0 will enter the critical
section before P1. Moreover, it will do so within a finite amount of time.
b. To demonstrate this fact, first consider process P0. Since turn = 0, it will only be waiting
for flag[1] to be set to false; more precisely, it will not be executing in the begin block
associated with the if statement. Furthermore, it will not change the value of flag[0].
Meanwhile, process P1 will find flag[0] = true and turn = 0. It will set flag[1] = false and wait
until turn = 1. At this point, P0 will enter its critical section. A symmetric argument can be
made if turn =1.

Das könnte Ihnen auch gefallen