GATE | GATE CS 1999 | Question 68
[5 Marks question]
a. A certain processor provides a ‘test and set’ instruction that is used as follows:
TEST register, flag
This instruction atomically copies flag to register and sets flag to 1. Give pseudo-code for implementing the entry and exit code to a critical region using this instruction.
b. Consider the following solution to the producer-consumer problem using a buffer of size 1. Assume that the initial value of count is 0. Also assume that the testing of count and assignment to count are atomic operations.
Producer: Repeat Produce an item; if count = 1 then sleep; place item in buffer. count = 1; Wakeup(Consumer); Forever Consumer: Repeat if count = 0 then sleep; Remove item from buffer; count = 0; Wakeup(Producer); Consume item; Forever;
Show that in this solution it is possible that both the processes are sleeping at the same time.
Quiz of this Question
Please comment below if you find anything wrong in the above post