edited by
403 views
3 votes
3 votes

Consider the following synchronization solution for Producer-Consumer problem. Here, counter is a shared variable initialized to 0, buffer is the shared memory region and increment and decrement operations take place atomically.

Consumer process
item nextConsumed;
while (1) {
    while (counter == 0);
    nextConsumed = buffer[out];
    out = (out + 1) % BUFFER_SIZE;
    counter--;
}
Producer process
item nextProduced;
while (1) {
    while (counter == BUFFER_SIZE);
    buffer[in] = nextProduced;
    in = (in + 1) % BUFFER_SIZE;
    counter++;
}


Which of the following is correct?

  1. Race condition may be possible.
  2. The solution works fine for Producer Consumer problem.
  3. Progress is violating.
  4. Buffer overflow is possible.
edited by

1 Answer

6 votes
6 votes
As both the increment and decrement operations are atomic the given solution works correctly. This is because the only time both Producer and Consumer access the same memory region is when the buffer is either full or empty (when in = out). But when buffer is empty, only producer can proceed and when buffer is full, only consumer can proceed.
Answer:

Related questions

2 votes
2 votes
1 answer
1
gatecse asked Feb 1, 2021
183 views
Consider a FAT file system where disk is divided into $M\;\text{byte}$ blocks, and every FAT entry can store an $N\;\text{bit}$ block number. What is the maximum size of ...
2 votes
2 votes
1 answer
4
gatecse asked Feb 1, 2021
142 views
Helioseismology is to Sun what Epidemiology is to ________EarthMedicineMoonDisease