797 views
1 votes
1 votes

Suppose that an operating system provides two functions, block_queue() which puts the calling process on the blocked queue, and wakeup_queue(P)which moves process P to the runnable queue if it is currently on the blocked queue (otherwise, its behaviour is unpredictable). Consider two processes P0 and P1 running the code given below.


void P0()
{ while(1) {
1.block_queue();
2.printf("P0");
3.wakeup_queue(P1);
}
}

void P1()
{ while(1) {
1.printf("P1");
2.wakeup_queue(P0);
3.block_queue();
}
}
Which of the Following statement is true?

  1.   it prints P0P1 alternatively always.
  2.   It prints P1P0 alternatively always
  3.   Deadlock possible
  4.   None

4 Answers

0 votes
0 votes
Consider a scenario process P1 arrives and it executes till the 2 instructions after this step P1 is preempted. Now process P0 arrives, its get block as it execute first instruction. Now after that process P1 returns and it execute instruction 3 and blocked. So now process P1 is waiting for process P0 & process P0 is waiting for process P1 to wake up. So clearly deadlock.

am i right??i have doubt,when P1 is preempted at line 2 where it perfomed wakeup P0,but at thta time P0 was not blocked,so wht would happen at tat time??

and will P1P0 will be printed alternayively always if there is no deadlock or can we have P0P1 also??
0 votes
0 votes
Suppose process P1  execute first then it will print P1 and go to next instruction in which it try to wakeup process P0 .Since P0 was not in  blocked queue therefore this wakeup call is wasted and P1 execute last instruction and get blocked and wait for wakeup call. Now P0 try to execute  and get blocked after first instruction  and wait for wakeup call. But the wakeup call for P0 is already wasted .Therefore both the processes are blocked and deadlock occur .

Please correct me if my explanation is wrong.
0 votes
0 votes

Execution of those two process will be as follows

Lets say both process start executing at the same time since there is no murex or such lock present ,

1- process P0 is blocking itself in its line 1 so it is waiting for wake up call which is a part of P1

2-P1 has no blocking call before printing so it will print P1 

3-P1 will wakeup P0

4-P1 will block itself

5-now P0 will get printed because P0 will start from the last instruction.

6-now P0 will wakeup P1 and again steps 1 to 6 will get repeated

so we can say that there is no deadlock but these function print P1P0 repeatedly

Related questions