试修改下面消费者生产者问题解法中的错误: producer: begin repeat produce

大学 解法,生产者,消费者 已帮助 人

试修改下面消费者生产者问题解法中的错误: 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
    producer:beginrepeatproduceaniteminnextp;wait(mutex);(wait(empty);)wait(empty);(wait(mutex);)buffer(in):=nextp;(in:=(in+1)roodn:)signal(mutex);(signal(full):)untilfalse;endconsumer:beginrepeatwait(mutex);(wait(full);)wait(full);(wait(mutex);)nextc:=buffer(out);out:=out+1;(out:=(out+1)modn;)signal(mutex);(signal(empty))consumeiterninnextc:untilfalse;end