《现代操作系统》严格轮转法没看懂
下面是引用原文
感觉这是错的。真的忙等待应该在while(turn!=0)后面加入分号。
while(TRUE){
while(turn!=0);
critical_regin();
turn=1;
noncritical_regin();
}
意思是不为0,我就一直等。是0了,我进入临界区,设置turn,进入非临界区。
是不是书错了?还是我理解错了 操作系统 进程互斥 严格轮转法
[解决办法]
我查了下英文版的,这段是这样写的:
Initially, process 0 inspects turn, finds it to be 0, and enters its critical region. Process 1 also finds it to be 0 and therefore sits in a tight loop continually testing turn to see when it becomes 1. Continuously testing a variable until some value appears is called busy waiting.
你的理解是正确的!但是作者写的也没错,但是容易混淆。其实它那个 /* loop */ 处就是说,这里是测试turn的忙等待的代码块。这里的代码是伪代码,不用受限于纯C的语法。
另外,别相信赵四那神棍的忽悠,他连semaphore和mutex都不懂呢,解决同步互斥还用global variable的手儿,水平还不如你呢。那本《30天自制操作系统》是个日本人写的玩具类科普书,也就他那水平的当圣经看。你读的这本书的作者是Tanenbaum,操作系统和网络方面大神级别的人物,Minux系统的发明人,也是当年linus的老师。如果你有兴趣,推荐你他的另一本书,《Operating Systems Design and Implementation (3rd Edition)》比这本还要著名。