直接上代碼
int random;
semaphore offer1 = 0;// 定義信號量對應的組合1
semaphore offer2 = 0;// 定義信號量對應的組合2
semaphore offer3 = 0;// 定義信號量對應的組合3
semaphore finish = 0;// 定義信號量表示抽菸是否完成
process P1() {// 生產者進程
while(1) {
random = 隨機整形數;
random = random%3;
if(random == 0)
V(offer1);
else if(random == 1)
V(offer2);
else
V(offer3);
任意兩種材料放在桌子上;
P(finish);// 此處用P幹嘛?P的目的不是請求資源嗎,此處不該是提供資源嗎?
}
}
process P2() {
while(1) {
P(offer3);
拿組合3對應的材料,捲菸,吸菸;
V(finish);// 繼續懵逼,此處用V幹嘛?如何阻止3個吸菸者的互斥?
}
}
process P3() {
while(1) {
P(offer2);
拿組合2對應的材料,捲菸,吸菸;
V(finish);// 繼續懵逼,此處用V幹嘛?如何阻止3個吸菸者的互斥?
}
}
process P4() {
while(1) {
P(offer1);
拿組合1對應的材料,捲菸,吸菸;
V(finish);// 繼續懵逼,此處用V幹嘛?如何阻止3個吸菸者的互斥?
}
}
疑惑:該算法如何用PV操作進行3個吸菸者的互斥的??