C 编程语言中的严格交替(来自 Tanenbaum)

发布于 2024-08-10 00:11:47 字数 384 浏览 4 评论 0原文

为什么不是严格交替测试进程 0 的第一个入口 while (turn == 0) //then Enter 进程0怎么能进入while (turn != 0),这不是和while (turn == 1)一样吗?

turn = 0;
//process 0 to enter
while (TRUE) {
  while (turn != 0)
    critical_region();
  turn = 1;
  noncritical_region();
}

//process 1 to enter
while (TRUE) {
  while (turn != 1)
    critical_region();
  turn = 0;
  noncritical_region();
}

Why isn't the test in strict alternation for the first entrance for process 0 while ( turn == 0) //then enter
How can process 0 enter while (turn != 0), is'nt this the same as while (turn == 1) ?

turn = 0;
//process 0 to enter
while (TRUE) {
  while (turn != 0)
    critical_region();
  turn = 1;
  noncritical_region();
}

//process 1 to enter
while (TRUE) {
  while (turn != 1)
    critical_region();
  turn = 0;
  noncritical_region();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-08-17 00:11:47

首先,你的代码是错误的。 Tanenbaum 的严格交替看起来像这样(他的示例是在没有块的情况下编写的,但我认为这种方式更容易遵循):

while (TRUE) {
    while (turn != 0)
    {
        /* do nothing */
    }
    critical_region();
    turn = 1;
    noncritical_region();
}

严格交替的想法有两个方面:

  1. 在任何给定的时刻,只有一个进程可以执行该函数Critical_region()
  2. 两个进程轮流运行ritical_region()(即,一旦进程0运行了ritical_region(),它必须等待进程1运行ritical_region(),然后才能再次运行)。

First off, you have the code wrong. Tanenbaum's strict alternation looks like this (his example are written without a block, but I think it's easier to follow this way):

while (TRUE) {
    while (turn != 0)
    {
        /* do nothing */
    }
    critical_region();
    turn = 1;
    noncritical_region();
}

The idea of strict alternation is two fold:

  1. At any given instant in time, only one process can be executing the function critical_region()
  2. The two processes take turns running critical_region() (i.e. once process 0 runs critical_region(), it must wait for process 1 to run critical_region() before being allowed to run it again).
_畞蕅 2024-08-17 00:11:47

如果您用括号展开您发布的代码,它看起来像这样:

turn = 0;
//process 0 to enter
while (TRUE) 
{
   while (turn != 0)
   {
      critical_region();
   }
   turn = 1;
   noncritical_region();
}

因此,第一次进入主循环时,它将 turn 设置为 1 并调用 nonritic_region。第二次它调用 ritic_region 并可能停留在那里。

If you expand the code you posted with brackets, it looks like this:

turn = 0;
//process 0 to enter
while (TRUE) 
{
   while (turn != 0)
   {
      critical_region();
   }
   turn = 1;
   noncritical_region();
}

So the first time you enter the main loop, it sets turn to 1 and calls noncritical_region. The second time it calls critical_region and presumably stays there.

思念绕指尖 2024-08-17 00:11:47

我认为代码实际上是这样的

while (TRUE)   
{  
while (turn != 0) **;**  
critical_region;  
turn=1;  
noncritical_region;  
}  

请注意,在书中,分号位于注释 /* Loop */ 后面,写在内部 while 之后。

I think the code is actually like this

while (TRUE)   
{  
while (turn != 0) **;**  
critical_region;  
turn=1;  
noncritical_region;  
}  

Note that in the book the semicolons are after the comments /* loop */ written there after the inner while.

別甾虛僞 2024-08-17 00:11:47

我怀疑正确的代码是:

turn = 0;
//process 0 to enter
while (TRUE) {
   while (turn != 0) critical_region();
   turn = 1;
   noncritical_region();
}

//process 1 to enter
while (TRUE) {
   while (turn != 1) critical_region();
   turn = 0;
   noncritical_region();
}

I suspect the correct code is:

turn = 0;
//process 0 to enter
while (TRUE) {
   while (turn != 0) critical_region();
   turn = 1;
   noncritical_region();
}

//process 1 to enter
while (TRUE) {
   while (turn != 1) critical_region();
   turn = 0;
   noncritical_region();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文