填空题:(6空)进程同步程序:哲学家问题之奇偶编号
完成程序:哲学家问题解决死锁的方法:奇数号哲学家先拿左边的叉子,偶数号哲学家先拿右边的叉子。
(空格处填写:wait, signal)
public class diningphilosophers {
semaphore [] fork = new semaphore[5](1);
int i;
public void philosopher (int i) {
while (true) {
think();
if (i % 2 == 1) {
(fork[i]);
(fork [(i+1) % 5]);
}
else{
(fork [(i+1) % 5]);
(fork[i]);
}
eat();
(fork [(i+1) % 5]);
(fork[i]);
}
}
public static void main() {
parbegin (philosopher (0),
philosopher (1),
philosopher (2),
philosopher (3),
philosopher (4));
}
}
答案:
第1空:wait ||
第2空:wait ||
第3空:wait ||
第4空:wait ||
第5空:signal ||
第6空:signal ||
(空格处填写:wait, signal)
public class diningphilosophers {
semaphore [] fork = new semaphore[5](1);
int i;
public void philosopher (int i) {
while (true) {
think();
if (i % 2 == 1) {
(fork[i]);
(fork [(i+1) % 5]);
}
else{
(fork [(i+1) % 5]);
(fork[i]);
}
eat();
(fork [(i+1) % 5]);
(fork[i]);
}
}
public static void main() {
parbegin (philosopher (0),
philosopher (1),
philosopher (2),
philosopher (3),
philosopher (4));
}
}
答案:
第1空:wait ||
第2空:wait ||
第3空:wait ||
第4空:wait ||
第5空:signal ||
第6空:signal ||