C 编程语言中的严格交替(来自 Tanenbaum)
为什么不是严格交替测试进程 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,你的代码是错误的。 Tanenbaum 的严格交替看起来像这样(他的示例是在没有块的情况下编写的,但我认为这种方式更容易遵循):
严格交替的想法有两个方面:
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):
The idea of strict alternation is two fold:
如果您用括号展开您发布的代码,它看起来像这样:
因此,第一次进入主循环时,它将
turn
设置为 1 并调用nonritic_region
。第二次它调用ritic_region
并可能停留在那里。If you expand the code you posted with brackets, it looks like this:
So the first time you enter the main loop, it sets
turn
to 1 and callsnoncritical_region
. The second time it callscritical_region
and presumably stays there.我认为代码实际上是这样的
请注意,在书中,分号位于注释 /* Loop */ 后面,写在内部 while 之后。
I think the code is actually like this
Note that in the book the semicolons are after the comments /* loop */ written there after the inner while.
我怀疑正确的代码是:
I suspect the correct code is: