Solution to Homework #3

6.2 Prove that, in the bakery algorithm (Section 6.2.2), the following property holds: If Pi is in its critical section and Pk (k 6 = i) has already chosen its number[k] 6 =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 Pi 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 Pi'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, Pk 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: var flag: array [0..1] of boolean; (* initially false *) turn: 0..1; repeat flag[i]:=true; while flag[j] do if turn = j then begin flag[i]:=false; while turn = j do no-op; flag[i]:=true; end; critical section turn := j; flag[i]:=false; remainder section

T2: P1 decrements S by 1 and enters critical section d. Since only Pi can update flag[j]. then mutual exclusion may be violated. the smoker needs three ingredients: tobacco. with Pj (j = 1 or 0) being the other process.until false.24. process P1 will find flag[0] = true and turn = 0.4 The first known correct software solution to the critical-section problem for n processes with a lower bound on waiting of n 6. A symmetric argument can be made if turn =1. Moreover.24 The structure of process Pi in Dekker's algorithm. If Pi finds flag[0] = false then it will enter the critical section. the result follows. we first note that the value of the variable turn is changed only at the end of the critical section. Since turn = 0. Two cases are now possible. Suppose that both processes want to enter the critical section. Answer: To prove property (a) we note that each Pi enters its critical section only if flag[j] = false. it will find flag[j]=false and immediately enter the critical section. and since Pi inspects flag[j] only while flag[i]=true. P0 will enter its critical section. T1: P2 determines that value of S =1 c. a. and matches. If Pi finds flag[0] = true then we claim that P0 will enter the critical section before P1. it will only be waiting for flag[1] to be set to false. Consider a system with three smoker processes and one agent process. is shown in Figure 6. T3: P3 decrements S by 1 and enters critical section 6. One of the smoker processes has paper. In this case. The smoker who has the remaining ingredient then makes . and the value of turn = 0. it will do so within a finite amount of time. Figure 6. more precisely. T0: P1 determines that value of S =1 b. Suppose that only process Pi wants to enter the critical section. Each smoker continuously rolls a cigarette and then smokes it. it will not change the value of flag[0]. It will set flag[1] = false and wait until turn = 1. 6. first consider process P0. paper.6 Show that. a. The agent has an infinite supply of all three materials. Meanwhile. But to roll and smoke a cigarette. The structure of process Pi (i = 0 or 1).8 The Cigarette-Smokers Problem. b. another has tobacco. To demonstrate this fact. if the wait and signal operations are not executed atomically. At this point. Answer: Suppose the value of semaphore S = 1 and processes P1 and P2 execute wait(S) concurrently. independent of the current value of turn. The agent places two of the ingredients on the table. To prove property (b). Prove that the algorithm satisfies all three requirements for the critical-section problem. Furthermore. and the third has matches. it will not be executing in the begin block associated with the if statement.

"smoke" signal(agent).. Write a program to synchronize the agent and the smokers... 6. X: condition. and the cycle repeats. Answer: The monitor code is type resource = monitor var P: array[0.2] of boolean. P2. until false. Each smoker process waits for the signal which informs the smoker to continue. wait(agent). signal(a[i])..13 Consider a system consisting of processes P1. until false. . Answer: The shared data structures are var a: array [0.wait(id) if not P[0] then printer-id := 0. using the priority numbers for deciding the order of allocation. signaling the agent on completion. The agent then puts out another two of the three ingredients. else printer-id := 2. else if not P[1] then printer-id := 1. repeat wait(a[i]). and one smoker can continue */ The agent process code is as follows: repeat Set i to a value between 0 and 2. begin if P[0] and P[1] and P[2] then X.. .2] of semaphore { initially = 0 } /* which controls which smoker can continue */ agent: semaphore { initially = 1 } /* Each time agent distribute two resources. procedure acquire (id: integer. each of which has a unique priority number. Write a monitor that allocates three identical line printers to these processes.and smokes a cigarette.. Pn. printer-id: integer).

end .signal.P[printer-id]:=true. end begin P[0] := P[1] := P[2] := false. end procedure release (printer-id: integer) begin P[printer-id]:=false. X.

Sign up to vote on this title
UsefulNot useful