操作系统:和尚打水问题某寺庙,有小和尚和老和尚若干,有一个水缸,由小和尚提水入缸供老和尚饮用.水缸可以容纳10桶水,水取

1个回答

  • Var mutex1, mutex2, empty, full, count: semaphore;

    mutex1:=1; mutex2:=1;

    empty:=10; full:=0; count:=3;

    process 小和尚:

    begin

    repeat

    wait(empty);

    wait(count);

    wait(mutex1);

    从井中取水;

    signal(mutex1);

    wait(mutex2);

    送水入水缸;

    signal(mutex2);

    signal(count);

    signal(full);

    until false;

    end

    process 老和尚:

    begin

    repeat

    wait(full);

    wait(count);

    wait(mutex2);

    从缸中取水;

    signal(mutex2);

    signal(empty);

    signal(count);

    until false;

    end