GATE | GATE-IT-2004 | Question 65
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.
 P1
while (1) {
   K;
P(mutex);
Add an item to the buffer;
V(mutex);
   L;
}
P2
while (1) {
  M;
P(mutex);
Remove an item from the buffer;
V(mutex);
   N;
}
The statements K, L, M and N are respectively
(A) P(full), V(empty), P(full), V(empty)
(B) P(full), V(empty), P(empty), V(full)
(C) P(empty), V(full), P(empty), V(full)
(D) P(empty), V(full), P(full), V(empty)
Answer: (D)
Explanation:
Process P1 is the producer and process P2 is the consumer.
Semaphore ‘full’ is initialized to ‘0’. This means there is no item in the buffer.
Semaphore ‘empty’ is initialized to ‘n’. This means there is space for n items in the buffer.
In process P1, wait on semaphore ’empty’ signifies that if there is no space in buffer then P1 can not produce more items. Signal on semaphore ‘full’ is to signify that one item has been added to the buffer.
In process P2, wait on semaphore ‘full’ signifies that if the buffer is empty then consumer can’t not consume any item. Signal on semaphore ’empty’ increments a space in the buffer after consumption of an item.
Thus, option (D) is correct.
Please comment below if you find anything wrong in the above post.
Quiz of this Question
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...