Semaphore S1 = 1; //1: semaphore taken
Semaphore S2 = 1;
P1:
Compute;
Take S1; //P1 blocks here until P2 gives S1
Use R1;
Use R2; //P1 uses R2 before P2
Give S2; //P1 gives S2, allowing P2 to use R2
P2:
Compute;
Use R1; //P2 directly uses R1
Give S1; //P2 gives S1, which will unblock P1
Take S2; //P2 blocks here until S2 is given by P1
Use R2;
1 回答
假设进程只运行一次,两个二进制信号量就足够了: