Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following proposed solution to dining philosopher's problem to avoid deadlock. Consider the binary semaphore lock in initialized to 1.

Philosopher (int i)
{  while (1)
   {  think ( );
      wait (lock); .............(i)
      wait (fork [i]);
      wait (fork [(i+1)%5]);
      signal (lock); ........ (ii)
      eat( );
      wait (lock); ............. (iii)
      signal (fork [i]);
      signal (fork (i + 1) % 5);
      signal (lock); ............ (iv)
}

Which of the following is correct ?

  • A.Removing (i) and (ii) will not affect the code. The code will work fine.
  • B.Removing (iii) and (iv) will not affect the code. The code will work fine.
  • C.Removing (i), (ii), (iii), (iv) will not affect the code. The code will work fine.
  • D.All (i), (ii), (iii), (iv) are necessary. Removal of any of them will affect the code.

Answer: B

The correct statement is that removing (iii) and (iv) will not affect the code.

The lock around (i) and (ii) makes picking up both forks a single atomic step: a philosopher cannot hold one fork while waiting for another, so the circular wait that causes deadlock cannot form. Removing (i) and (ii) would allow every philosopher to take the left fork at once and deadlock.

Releasing forks needs no such protection. Signalling a semaphore never blocks, so putting the forks down one after the other cannot cause deadlock, and the lock around (iii) and (iv) is unnecessary. That rules out options A, C and D.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.