问答题 试修改下面消费者生产者问题解法中的错误: producer: begin repeat produce an item in nextp; wait(mutex); wait(empty); buffer(in):=nextp; signal(mutex); until false; end consumer: begin repeat wait(mutex); wait(full); nextc:=buffer(out); out:=out+1; signal(mutex); consume item in nextc; until false; end
【正确答案】正确答案:producer: begin repeat produce an item in nextp; wait(mutex);(wait(empty);) wait(empty);(wait(mutex);) buffer(in):=nextp; (in:=(in+1)rood n:) signal(mutex); (signal(full):) until false; end consumer: begin repeat wait(mutex);(wait(full);) wait(full);(wait(mutex);) nextc:=buffer(out); out:=out+1;(out:=(out+1)mod n;) signal(mutex); (signal(empty)) consume itern in nextc: until false; end
【答案解析】