(a) A certain processor provides a ‘test and set’ instruction that is used as…

PYQ GATE 1999

(a) A certain processor provides a ‘test and set’ instruction that is used as follows:

TSET register, flag

This instruction atomically copies flag to register and sets flag to 1. Give pseudocode 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 processes are sleeping at the same time.

Sign up free to check your answer

Sign up free

Explore the full course: Operating System

Loading lesson…