Q124: Cycle in Resource Allocation Graphs

Question #124: Which of the following about a cycle in process resource allocation graphs is false?

Options:

  1. Any cycle depicts a deadlock.
  2. If all the resources have single instance, then the cycle depicts a deadlock.
  3. If all instances of a resource are allocated to a process in a cycle, then there is deadlock.
  4. If the cycle involves only a set of resource types, each of which has only a single instance, then a deadlock has occurred.

Solution: The correct answer is first one. As seen the question above, if resources have multiple instances, then cycle may or may not depict a deadlock.

Q122: Plotting processes III

This  question is in continuation with questions #117 & #119.

Question #122: Is there any way by which both processes can finish without having deadlock.

Options:

  1. No, there will always be a deadlock.
  2. Yes, A and B can proceed in any way, and there won’t be a deadlock ever.
  3. After reaching ‘t’, A has to proceed till I4 and only then B should proceed.
  4. After reaching ‘t’, A can reach till I3, then B can reach till I8 and then A can finish.

Solution: The correct answer is option 3rd. Either A or B has to finish first and only then other process should start to avoid deadlock.

Q119: Plotting processes II

This question is in continuation of question #117.

Question #119: What is true for rectangle I1 I2 I5 I6?

Options:

  1. System can never enter this state.
  2. System can enter this state but there will eventually be a deadlock.
  3. This is a deadlock state.
  4. System can enter into this state and can eventually reach ‘u’ (finish the job).

Solution: The correct answer is 2nd one. Once the system enters this state, it means that A has acquired printer and B has acquired plotter. Now after some time, A needs plotter and B needs printer, hence there will be a deadlock in the system.

Q117: Plotting processes I

shaded_regions

The above figure depicts two processes A and B on the two axis, and there is one plotter and one printer in the system. Process A needs printer from time I1 to I3 and needs plotter from time I2 to I4. Similarly, the timeline is shown for Process B where it needs plotter from I5 to I7 and printer from I6 to I8. Answer the following questions based on the given information.

 

Question #117: What do the shaded regions indicate?

1. Deadlock

2. Mutual exclusion not satisfied

3. Safe Region

4. None of the above

Solution: The correct answer is 2nd one. Since there is only one instance of printer, it must not be shared by both processes for mutual exclusion.

 

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.

Q111: How can priority inversion be handled?

You can checkout question #108 to know what is priority inversion.

Question #111: How can priority inversion be handled?

Options:

  1. By raising the priority of the blocked process.
  2. By lowering the priority of the blocked process.
  3. By raising the priority of the lower priority process for which higher priority process is waiting.
  4. Both 2 and 3.

Solution: The correct answer is 4th one. If we let lower priority process run by temporarily raising its priority, it will release the semaphore quickly. This is called priority inheritance. This might be needed at various steps in the chain and not just once.

Q47: Safe State

A “safe state” is a state such that there is some possible execution sequence following which all the processes can run to completion and deadlock can be avoided. Consequently, system could be in “unsafe state” in which case, there is no deadlock free sequence of resource allocation.

Question #47: Given the data below, which of the following are safe & unsafe states? Resource has 10 instances in total. There are 3 processes which are currently holding “Has” number of resources & in total need “Wants” number of resources to complete execution.

P12

Options:

A) Both State 1 and State 2 are safe states.

B) Both State 1 and State 2 are unsafe states.

C) State 1 is Safe and State 2 is unsafe.

D) State 1 is Unsafe and State 2 is safe.

Solution:  In state 1, total allocated resources are 7, making 3 resources  freely available. This is enough to cater process B, then process C and finally process A. We cannot find a sequence for State 2. Hence, C is the correct solution.

Q35: fork() || fork() && fork()

Question #35: How many total processes will be spawned after executing following program?

int main()

{

fork() || fork() && fork() ;

}

 

Options:

A)     4

B)      8

C)      6

D)     7

 

Solution:

As we have OR after the first fork(), hence both child and parent process will be executed further. After second fork(), we have 4 processes. In one of the case, where in we have child process in each of the fork calls, we have 0 || 0 = 0 till now, hence the next fork() won’t be executed and will return from here. For all other cases, another child will be forked, hence 4+3 = 7 processes. Hence, the correct answer is option D.

Q32: fork && fork

Question #32: How many total processes will be spawned after executing following program?

int main()

{

fork() && fork();

}

A)     2

B)      3

C)      4

D)     1

Solution: The system call fork() returns PID for the parent but returns 0 for the child. So after the first fork(), the child process returns 0 and will not execute the next fork since there is a logical AND operator after that. 0 AND with anything is 0, hence the next fork() won’t be executed, there by totaling 3 processes. Hence, the correct answer is B.

Q31: fork system call

Question #31: The system call fork() returns PID for the parent but returns 0 for the child. How many total processes will be spawned after executing following program?

int main()

{

fork();

fork();

}

A)     2

B)     3

C)     4

D)     1

 

Solution: After first fork(), we have two processes, let’s say A(parent) and B(child). Now, both A and B will fork one child each, hence totaling 4 processes. Therefore, the correct answer is C.