image.png
這里是多類生產者,生產多種商品,并且有多個消費者消費。
多類生產者和多類消費者問題
semaphore mutex =1; //盤子是臨界資源 互斥
semaphore apple = 0; //同步信號量 蘋果 爸爸->女兒
semaphore orange = 0; //同步信號量 橘子 媽媽->兒子
semaphore empty = 1;//同步信號量 盤子 兒子->媽媽,女兒->爸爸
father{
準備蘋果;
P(empty);
P(mutex);
使用盤子,放蘋果;
V(mutex);
V(apple);
}
daughter{
P(apple);
P(mutex);
使用盤子,取蘋果;
V(mutex);
V(empty);
吃掉蘋果;
}
mother{
準備橘子;
P(empty);
P(mutex);
使用盤子,放橘子;
V(mutex);
V(orange);
}
son{
P(orange);
P(mutex);
使用盤子,取橘子;
V(mutex);
V(empty);
吃掉橘子;
}