Dining-Philosophers Problem
?Shared data ?Semaphore chopStick[] = new Semaphore[5]; Philosopher(i) Philosopher(i)
Repeat 思考; 取chopStick[i];
取chopStick[(i+1) mod 5]; 进餐; 放chopStick[i];
放chopStick[(i+1) mod 5]; Until false;
Dining-Philosophers Problem (Cont.)
Philosopher i: while (true) { // get left chopstick chopStick[i].P(); // get right chopstick chopStick[(i + 1) % 5].P(); // eat for awhile //return left chopstick chopStick[i].V(); // return right chopstick chopStick[(i + 1) % 5].V(); // think for awhile }
讨论
可能会出现死锁,五个哲学家每人拿起了他左边的筷子
01 最多允许四个哲学家同时就坐 02 同时拿起两根筷子 03 非对称解决
操作系统02-6.4 哲学家问题 - 16 - 图文
Dining-PhilosophersProblem?Shareddata?SemaphorechopStick[]=newSemaphore[5];Philosopher(i)Philosopher(i)Repeat思考;取chopStick[i];取chopStick[(i+1)mod5
推荐度:
点击下载文档文档为doc格式