学习Peterson’s Solution

Peterson’s Solution

引用部分来自书籍Silberschatz’s Operating System Concepts, 9th Edition

Peterson’s Solution
we illustrate a classic software-based solution to the
critical-section problem known as Peterson’s solution. Because of the
way modern computer architectures perform basic machine-language
instructions, such as load and store, there are no guarantees that
Peterson’s solution will work correctly on such architectures.
However, we present the solution because it provides a good
algorithmic description of solving the critical-section problem and
illustrates some of the complexities involved in designing software
that addresses the requirements of mutual exclusion, progress, and
bounded waiting.

对于临界区问题的解答1)基于硬件的 2)基于软件的(如Peterson算法)。

 do { 
        flag[i] = true;                  //Pi想要进入临界区
        turn = j;                        //turn 的值是j Pj可进入临界区
        while (flag[j] && turn == j);    //检查Pj是否想进入临界区而且Pj 是否可进入临界区,同时满足两个条件Pj就占用临界区
        critical section                 /*临界区*/
        flag[i] = false;                 //Pi用完临界区设置flag[i] 为false
        remainder section                /*剩余区*/
        } while (true);
//  Figure 5.2 The structure of process Pi in Peterson’s solution.

Peterson’s solution is restricted to two processes that alternate
execution between their critical sections and remainder sections. The
processes are numbered P0 and P1. For convenience, when presenting Pi
, we use Pj to denote the other process; that is, j equals 1 − i.

Peterson算法:
适用于两个进程(Pi && Pj)在临界区和剩余区间的交替执行
此算法要求Pi 和 Pj 共享两个数据项 :

  1. int turn; (变量turn用来表示哪个进程可以进入临界区)
  2. boolean flag[2]; (数组flag表示哪个进程想要进入临界区)

Peterson’s solution requires the two processes to share two data items:
int turn;
boolean flag[2];

The variable turn indicates whose turn it is to enter its critical
section. That is, if turn == i, then process Pi is allowed to execute
in its critical section. The flag array is used to indicate if a
process is ready to enter its critical section. For example, if
flag[i] is true, this value indicates that Pi is ready to enter its
critical section. With an explanation of these data structures
complete, we are now ready to describe the algorithm shown in
Figure5.2.

process Pi 为了进入临界区,

  1. Pi 设置 flags[i] == true
  2. 且 Pi 设置 turn == j
    表示如果 Pj 想要进入临界区,那么Pj可以进入

如果两个进程想同时进入临界区,那么turn会被设置成 i 和 j,但只有一个赋值语句结果会保持,另一个也会被设置但很快被重写,turn的值决定了哪个进程能允许进入临界区

To enter the critical section, process Pi first sets flag[i] to be
true and then sets turn to the value j, thereby asserting that if the
other process wishes to enter the critical section, it can do so. If
both processes try to enter at the same time, turn will be set to both
i and j at roughly the same time. Only one of these assignments will
last; the other will occur but will be overwritten immediately. The
eventual value of turn determines which of the two processes is
allowed to enter its critical section first.

证明解决方法的正确性:

We now prove that this solution is correct. We need to show that:

  1. Mutual exclusion is preserved.
  2. The progress requirement is satisfied.
  3. The bounded-waiting requirement is met.

To prove property 1,
we note that each Pi enters its critical section
only if either flag[j] == false or turn == i. Also note that, if both
processes can be executing in their critical sections at the same
time, then flag[0] == flag[1] == true. These two observations imply
that P0 and P1 could not have successfully executed their while
statements at about the same time, since the value of turn can be
either 0 or 1 but cannot be both. Hence, one of the processes —say, Pj
—must have successfully executed the while statement, whereas Pi had
to execute at least one additional statement (“turn == j”). However,
at that time, flag[j] == true and turn == j, and this condition will
persist as long as Pj is in its critical section; as a result, mutual
exclusion is preserved.

To prove properties 2 and 3, we note that a process Pi can be
prevented from entering the critical section only if it is stuck in
the while loop with the condition flag[j] == true and turn == j; this
loop is the only one possible. If Pj is not ready to enter the
critical section, then flag[j] == false, and Pi can enter its critical
section. If Pj has set flag[j] to true and is also executing in its
while statement, then either turn == i or turn == j. If turn == i,
then Pi will enter the critical section. If turn == j, then Pj will
enter the critical section. However, once Pj exits its critical
section, it will reset flag[j] to false, allowing Pi to enter its
critical section. If Pj resets flag[j] to true, it must also set turn
to i. Thus, since Pi does not change the value of the variable turn
while executing the while statement, Pi will enter the critical
section (progress) after at most one entry by Pj (bounded waiting).


学习Peterson’s Solution的摘录和笔记,帮助自己理解,故记录下来。–牵着蜗牛去爬山

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值