【发布时间】:2015-09-14 02:08:20
【问题描述】:
我正在阅读著名的操作系统概念书(Avi Silberschatz、Peter Baer Galvin、Greg Gagne)第 9 版:http://codex.cs.yale.edu/avi/os-book/OS9/
在进程同步章节中,有一个“有界等待互斥与test_and_set”的算法如下:
do {
waiting[i] = true;
key = true; // <-- Boolean variable that I do not see its utility
while (waiting[i] && key) // <-- the value of the key variable here is always true
key = test_and_set(&lock); // <-- it might become false here, but what is the point?
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
waiting[j] = false;
/* remainder section */
} while (true);
我看不到布尔变量key的作用(在上面代码的第3、4和5行使用),我看到我们可以删除它而不会对算法产生任何特殊影响,是吗?对还是我错过了什么?
【问题讨论】:
-
key是这里用来退出while循环的两种方式之一,如果lock则设置为false > 是 false 并且使当前进程能够执行其关键部分。
标签: operating-system synchronization locking mutual-exclusion