生産者/消費者問題を解決する方法を知りたいのですが、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
}
}
そのような「擬似コード」を使用して、2人の消費者で同じ問題(最後がOKの場合)を解決するために知りたいです。