怎么做网站文件,网站建设的方案计划,婚庆策划公司名称,微信商城平台开发1.最多允许四个哲学家同时进餐#xff0c;保证有一个筷子是空闲的#xff0c;从而保证能有有一个哲学家成功进餐#xff0c;而不导致死锁
semaphore chopstick[5] {1, 1, 1, 1, 1}, mutex4;
Pi(){do{think...P(mutex);P(chopstick[i]);P(chopstick[(i1)%5);eat...V(mutex)…1.最多允许四个哲学家同时进餐保证有一个筷子是空闲的从而保证能有有一个哲学家成功进餐而不导致死锁
semaphore chopstick[5] {1, 1, 1, 1, 1}, mutex4;
Pi(){do{think...P(mutex);P(chopstick[i]);P(chopstick[(i1)%5);eat...V(mutex);V(chopstick[i]);V(chopstick[(i1)%5);}while(1);
}2.给哲学家编号要求奇数哲学家先拿左边的筷子再拿右边的筷子而偶数哲学家相反
semaphore chopstick[5] {1,1,1,1,1}; //初始化信号量void philosopher(int i){do {thinking... if(i%2 1){P(chopstick[i]);//判断哲学家左边的筷子是否可用P(chopstick[(i1)%5]);//判断哲学家右边的筷子是否可用}else{P(chopstick[(i1)%5]);//判断哲学家右边的筷子是否可用P(chopstick[i]);//判断哲学家左边的筷子是否可用}eat...V(chopstick[i]);V(chopstick[(i1)%5]);}while(1);
}3.一名哲学家左右两边的筷子均可用 时才允许进餐
semaphore chopstick[5] {1, 1, 1, 1, 1},mutex1;
Pi(){do{think...P(mutex); //使用一个互斥信号量保证拿起左右两边的筷子是一气呵成的操作P(chopstick[i]);p(chopstick[(i1)%5]);V(mutex);eating...V(chopstick[i]);V(chopstick[(i1)%5]);}while(1);
}