问答题
试修改下面生产者一消费者问题解法中的错误。 producer: begin repeat …… producer an item in nextp; wait(mutex): wait(full): buffer(in):=nextp; signal(mutex); until false; end consumer: begin repeat wait(mutex); wait(empty); nextc:=buffer(out); out:=out+1; signal(mutex); consumer item in nextc; until false; end
【正确答案】正确答案:producer: begin repeat … producer an item in nextp; wait(mutex); wait(full); /*应为wait(empty),而且还应该在wait(mutex)的前面*/ buffer(in):=nextp; /*缓冲池数组游标应前移:in:=(in+1)mod n;*/ signal(mutex); /*signal(full);*/ until false; end consumer; begin repeat wait(mutex); wait(empty); /*应为wait(full),而且还应该在wait(mutex)的前面*/ nextc:=buffer(out); out:=out+1; /*考虑循环,应改为:out:=(out+1)mod n;*/ signal(mutex); /*signal(empty);*/ consumer item in nextc; until false; end