GATE 2005
203 questions
MCQ 2031-mark 203
Questions 151–155 of 203
Two concurrent processes P1 and P2 use four shared resources R1, R2, R3 and R4, as shown below.
Both processes are started at the same time, and each resource can be accessed by only one process at a time The following scheduling constraints exist between the access of resources by the processes:
P2 must complete use of R1 before P1 gets access to R1.
P1 must complete use of R2 before P2 gets access to R2.
P2 must complete use of R3 before P1 gets access to R3.
P1 must complete use of R4 before P2 gets access to R4.
There are no other scheduling constraints between the processes. If only binary semaphores are used to enforce the above scheduling constraints, what is the minimum number of binary semaphores needed?
Both processes are started at the same time, and each resource can be accessed by only one process at a time The following scheduling constraints exist between the access of resources by the processes:
P2 must complete use of R1 before P1 gets access to R1.
P1 must complete use of R2 before P2 gets access to R2.
P2 must complete use of R3 before P1 gets access to R3.
P1 must complete use of R4 before P2 gets access to R4.
There are no other scheduling constraints between the processes. If only binary semaphores are used to enforce the above scheduling constraints, what is the minimum number of binary semaphores needed?
Answer: (B)
Each process uses R1, R2, R3, R4 in this order. The four rules fix who goes first on each resource: P2 on R1, P1 on R2, P2 on R3, P1 on R4. So the turn passes P2 to P1, then P1 to P2, then P2 to P1, then P1 to P2.
Use two semaphores, both starting at 0: means "P2 is done, P1 may go", and means "P1 is done, P2 may go".
P1: P(X); use R1; use R2; V(Y); P(X); use R3; use R4; V(Y)
P2: use R1; V(X); P(Y); use R2; use R3; V(X); P(Y); use R4
Why each rule holds:
R1: P1 waits on X, and P2 does V(X) right after R1.
R2: P2 waits on Y, and P1 does V(Y) right after R2.
R3: P1 waits on X again, and P2 does V(X) right after R3.
R4: P2 waits on Y again, and P1 does V(Y) right after R4.
Each V is done only after the earlier signal on that semaphore has been taken, so X and Y never need a value above 1. Binary semaphores are enough.
One semaphore is not enough. The signals go in both directions, and with a single semaphore the process that signals could take its own signal back with a later P. Then the other process would not be forced to go first. So each direction needs its own semaphore, which gives a minimum of 2.
Answer: (B)
Use two semaphores, both starting at 0: means "P2 is done, P1 may go", and means "P1 is done, P2 may go".
P1: P(X); use R1; use R2; V(Y); P(X); use R3; use R4; V(Y)
P2: use R1; V(X); P(Y); use R2; use R3; V(X); P(Y); use R4
Why each rule holds:
R1: P1 waits on X, and P2 does V(X) right after R1.
R2: P2 waits on Y, and P1 does V(Y) right after R2.
R3: P1 waits on X again, and P2 does V(X) right after R3.
R4: P2 waits on Y again, and P1 does V(Y) right after R4.
Each V is done only after the earlier signal on that semaphore has been taken, so X and Y never need a value above 1. Binary semaphores are enough.
One semaphore is not enough. The signals go in both directions, and with a single semaphore the process that signals could take its own signal back with a later P. Then the other process would not be forced to go first. So each direction needs its own semaphore, which gives a minimum of 2.
Answer: (B)
Given below is a program which when executed spawns two concurrent processes :
semaphore X : = 0 ;
/* Process now forks into concurrent processes P1 & P2 */
Consider the following statements about processes P1 and P2:
I.It is possible for process P1 to starve.
II.It is possible for process P2 to starve.
Which of the following holds?
semaphore X : = 0 ;
/* Process now forks into concurrent processes P1 & P2 */
Consider the following statements about processes P1 and P2:
I.It is possible for process P1 to starve.
II.It is possible for process P2 to starve.
Which of the following holds?
Answer: (A)
Take the classical busy-wait definition of the operations, with at the start. P(X): loop while , then do . V(X): do . A process starves when it keeps trying to pass P(X) but is overtaken every time.
P2 can starve (statement II). P2 passes P(X) only if it tests X while . Let P1 repeat V(X) (), Compute, P(X) (). Let P2 run only just after P1's P(X), when . P2 then sees 0 every time and keeps looping, while P1 goes on forever.
P1 can starve (statement I). P1 does V(X), so . P2 does P(X), so , and then Compute. P1 reaches P(X) and sees 0, so it loops. Now P2 does V(X), so , and goes straight to its next P(X), so again before P1 tests X. P1 only ever sees 0. This can repeat for ever.
Both statements are true.
Answer: (A)
P2 can starve (statement II). P2 passes P(X) only if it tests X while . Let P1 repeat V(X) (), Compute, P(X) (). Let P2 run only just after P1's P(X), when . P2 then sees 0 every time and keeps looping, while P1 goes on forever.
P1 can starve (statement I). P1 does V(X), so . P2 does P(X), so , and then Compute. P1 reaches P(X) and sees 0, so it loops. Now P2 does V(X), so , and goes straight to its next P(X), so again before P1 tests X. P1 only ever sees 0. This can repeat for ever.
Both statements are true.
Answer: (A)
Two shared resources and are used by processes and . Each process has a certain priority for accessing each resource. Let denote the priority of for accessing . A process can snatch a resource from process if is greater than .
Given the following :
(I).
(II).
(III).
(IV).
Which of the following conditions ensures that and can never deadlock?
Given the following :
(I).
(II).
(III).
(IV).
Which of the following conditions ensures that and can never deadlock?
Answer: (C)
Snatch rule: can take from only if . A process that can snatch any resource it needs never waits for the other one, so it cannot be in a deadlock.
(C) (I) and (II): and . So has the higher priority on both resources and can snatch either from . It will finish and release all its resources, and then can proceed. No deadlock.
(A) (I) and (IV): holds and waits for , which holds. cannot snatch it because . waits for and cannot snatch it because . Both wait for each other, so this is a deadlock.
(B) (II) and (III): holds and waits for , which holds. cannot snatch it because . waits for and cannot snatch it because . So a deadlock can occur here too.
Answer: (C)
(C) (I) and (II): and . So has the higher priority on both resources and can snatch either from . It will finish and release all its resources, and then can proceed. No deadlock.
(A) (I) and (IV): holds and waits for , which holds. cannot snatch it because . waits for and cannot snatch it because . Both wait for each other, so this is a deadlock.
(B) (II) and (III): holds and waits for , which holds. cannot snatch it because . waits for and cannot snatch it because . So a deadlock can occur here too.
Answer: (C)
Suppose n processes, share m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process is , where . Which one of the following is a sufficient condition for ensuring that deadlock does not occur?
Answer: (C)
Deadlock needs all units to be held while every process still needs more. A process that is still waiting holds at most units. So all processes together hold at most units.
If this is less than , at least one unit is always free. Then some process can get its last unit, finish and release everything, and the rest can finish the same way. So no deadlock if
.
Why the others fail (take and every ): each process holds 1 unit, none is free, and each needs one more, so this is a deadlock. Yet (A) and (B) both hold. For (D), also holds, so (D) is not enough either.
Answer: (C)
If this is less than , at least one unit is always free. Then some process can get its last unit, finish and release everything, and the rest can finish the same way. So no deadlock if
.
Why the others fail (take and every ): each process holds 1 unit, none is free, and each needs one more, so this is a deadlock. Yet (A) and (B) both hold. For (D), also holds, so (D) is not enough either.
Answer: (C)
We wish to schedule three processes P1, P2 and P3 on a uniprocessor system. The priorities, CPU time requirements and arrival times of the processes are as shown below.
We have a choice of preemptive or non-preemptive scheduling. In preemptive scheduling, a late-arriving higher priority process can preempt a currently running process with lower priority. In non-preemptive scheduling, a late-arriving higher priority process must wait for the currently executing process to complete before it can be scheduled on the processor.
What are the turnaround times (time from arrival till completion) of P2 using preemptive and non-preemptive scheduling respectively?
We have a choice of preemptive or non-preemptive scheduling. In preemptive scheduling, a late-arriving higher priority process can preempt a currently running process with lower priority. In non-preemptive scheduling, a late-arriving higher priority process must wait for the currently executing process to complete before it can be scheduled on the processor.
What are the turnaround times (time from arrival till completion) of P2 using preemptive and non-preemptive scheduling respectively?
Answer: (D)
Turnaround time = completion time minus arrival time.
Non-preemptive: arrives at 0 and runs 0 to 15. At 15, (priority 10) and (priority 9) are waiting, so runs 15 to 35. Then runs 35 to 45.
turnaround = sec.
Preemptive:
- 0 to 3: runs (12 sec left).
- 3 to 5: arrives with higher priority and runs (8 sec left).
- 5 to 25: arrives with the highest priority and runs to completion.
- 25 to 33: finishes.
- 33 to 45: finishes.
turnaround = sec.
So the turnaround times of are 30 sec (preemptive) and 42 sec (non-preemptive).
Answer: (D)
Non-preemptive: arrives at 0 and runs 0 to 15. At 15, (priority 10) and (priority 9) are waiting, so runs 15 to 35. Then runs 35 to 45.
turnaround = sec.
Preemptive:
- 0 to 3: runs (12 sec left).
- 3 to 5: arrives with higher priority and runs (8 sec left).
- 5 to 25: arrives with the highest priority and runs to completion.
- 25 to 33: finishes.
- 33 to 45: finishes.
turnaround = sec.
So the turnaround times of are 30 sec (preemptive) and 42 sec (non-preemptive).
Answer: (D)