edited by
6,989 views
30 votes
30 votes

The semaphore variables full, empty and mutex are initialized to $0$, $n$ and $1$, respectively. Process P1 repeatedly adds one item at a time to a buffer of size $n$, and process P2 repeatedly removes one item at a time from the same buffer using the programs given below. In the programs, $K$, $L$, $M$ and $N$ are unspecified statements.

$P_1$
while (1) {
    K;
    P(mutex);
    Add an item to the buffer;
    V(mutex);
    L;
}
$P_2$
while (1) {
    M;
    P(mutex);
    Remove an item from the buffer;
    V(mutex);
    N;
}

The statements $K$, $L$, $M$ and $N$ are respectively

  1. P(full), V(empty), P(full), V(empty)
  2. P(full), V(empty), P(empty), V(full)
  3. P(empty), V(full), P(empty), V(full)
  4. P(empty), V(full), P(full), V(empty)
edited by

6 Answers

1 votes
1 votes
$full=0$ means there is no item in the buffer

$empty = n$ means there are n available spaces in buffer for items

so producer i.e. $p_1$ must produce items first as down(wait) operation  can be only performed on empty

so$ K=P(empty)$

Consumer i.e $p_2$ should never  start consuming empty buffer as initially buffer is empty so $p_2$ should never start first to ensure this perform down on full as full=0 so consumer will get stuck till producer performs up on full

so $ M=P(full)$

$ L=V(full)$

after consumer consumes items i.e buffer has capacity to keep items it should let producer produce more items

so $ N=V(empty)$
Answer:

Related questions

28 votes
28 votes
4 answers
4