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.

Q61: Atomicity of semaphore operations

Question 61: What is true about up() and down() operations of semaphore?

  1. The operations up() and down() must have to be atomic.
  2. The operations up() and down() can be made atomic by using the hardware based solution like TSL.
  3. The semaphore variable is a user level variable, but can’t be accessed directly.
  4. Both options 1 and 2.

 

Solution: They have to be atomic otherwise there can be race conditions. Hence, the correct answer is option 4.