试修改下面生产者一消费者问题解法中的错误。 producer; begin repeat producer an item in nextp; wait(mutex); wait(full); buf

admin2019-01-16  34

问题 试修改下面生产者一消费者问题解法中的错误。
    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

解析
转载请注明原文地址:https://kaotiyun.com/show/wiRi777K
0

最新回复(0)