Q113: Deadlock while acquiring resources

Question #113: Can there be a deadlock in the following piece of code?


semaphore resource_1;

semaphore resource_2;

void process_A(void) {

  down(&resource_1);

  down(&resource_2);

  use_both_resources( );

  up(&resource_2);

  up(&resource_ 1);

}
void process_B(void) {

  down(&resource_1);

  down(&resource_2);

  use_both_resources( );

  up{&resource_2);

  up(&resaurce_1);

}
  1. Yes, process A and process B can both try to acquire resource_1.
  2. No, it is a deadlock free code.
  3. Yes, there can be a deadlock while releasing resources.
  4. None of the above.

Solution: The correct answer is option 2. Any process who acquired the resource_1 first will proceed with the critical section and then the other process will proceed as soon as it releases resource_1.