简单清晰地生产者—消费者模型

首先需要用三个量来控制

ini:            //初始化
mutex = 1       //互斥锁,同一时间只能有一个读取
empty = n       //现有空位数
full = 0        //现有库存数

将empty和full分开其实体现了生产者与消费者行为之间的互相影响。

producer:
    wait(empty);    //首先等empty>0,然后empty-1
    wait(mutex);    //等mutex=1
    //place in buffer
    sig(mutex);     //恢复锁
    sig(full);     //更新库存

consumer:
    wait(full);
    wait(mutex);
    //place in buffer
    sig(mutex);
    sig(empty);

评论

暂无评论