·图5.3每个人有一个自已的小屋(P199) ·每个人只能检查,但不能修改其他人的 黑板标志 ·若某人申请进入临界区,首先检查对方 黑板是否为“ false” 是,修改自己小屋黑板值为“true” 进入临界区执行,完毕,恢复黑板值为 “ false” 否,反复进入小屋,检査黑板标志,直 到标志是“ false
是,修改自己小屋黑板值为“true” , 进入临界区执行,完毕,恢复黑板值为 “false” 否,反复进入小屋,检查黑板标志,直 到标志是“false
tru 木a Figure 5.3 A Two-lgkc:o Solution for Mutual Exclusion
var flag: array|0.1 of boolean:fale;{共享全局变 量} PROCESS O PROCESS 1 while flaglll while flago do nothing; do nothing; fag: true fagl: = true; <critical section> <critical section> flag: false; flag1: = false;
• var flag : array [0..1] of boolean :false ; {共享全局变 量} PROCESS 0 PROCESS 1 … … while flag[1] while flag[0] do {nothing}; do {nothing}; flag[0]:=true; flag[1]:=true; <critical section>; <critical section> flag[0]:=false; flag[1]:=false; … …
若进程执行完临界区,恢复自已标志为 “ false”失败,则其他进程永久阻塞。 This method does not guarantee mutual exclusion 分析以下执行顺序: P0:当flag[1]= false;执行 while flag[1] P1:当flag[0]= false;执行 while flag[0]; P0:置flag[0]=true执行< critical section>; P1:置flag[1]=true;执行< critical section>;
Dekker’ s Algorithn ★ Third Attempt(第3种尝试) Set flag to enter critical section before check other processes If another process is in the critical section when the flag is set, the process is blocked until the other process releases the critical section