0
我想知道如何解決生產者/消費者問題,但使用2個不同的消費者,我也需要知道如何使用嚴格的交替來解決它。解決生產者/消費者問題的算法
我做了如下算法1個生產者和1名消費者
producer()
{
while(true)
{
if i == N //full buffer
turn = 1
while turn <> 0
{
// nothing
}
produceitem(&item)//produce the item
insertitem(item, buffer)//insert the item in the buffer
turn = 1
//process zone
}
}
consumer()
{
while(true)
{
if i == 0 //empty buffer
turn = 0
while turn <> 1
{
// nothing
}
consumeitem(&item)
deleteitem(buffer)//erase the item from buffer
turn = 0
//process zone
}
}
使用那種「僞代碼」我想知道,以解決同樣的問題(如果最後是OK)2消費者。
第二個函數肯定應該命名爲'consumer()'。 –