Sei sulla pagina 1di 19

CS F372: Operating Systems

Lect 24: Deadlock Avoidance

Chittaranjan Hota
BITS Pilani Professor, Dept. of Computer Sc. & Information Systems
hota@hyderabad.bits-pilani.ac.in
Hyderabad Campus
Deadlock Avoidance

Two process resource trajectories


Example: Is state in (a) safe?

(a) (b) (c) (d) (e)


Example continued

(a) (b) (c) (d)

If A asks for a resource, what will you do?

It is worth noting that an unsafe state is not a


deadlocked state. Why, argue?
The Banker's Algorithm for a
Single Resource

(a) (b) Safe? (c) Safe?

Let Work and Finish be vectors of length m


Let us see the algorithm on
and n, respectively. We will use Available,
the board Allocation and Need matrices/vectors.
Banker's Algorithm for Multiple Resources

Example
If B requests a Scanner, can it be granted?
After giving B a Scanner, if E requests the last Scanner,
can it be granted?
Deadlock Avoidance Advantages &
Restrictions
It is not necessary to pre-empt and rollback
processes, as in deadlock detection & resolution.

Maximum resource requirement must be


stated in advance.
Processes under consideration must be
independent and with no synchronization
requirements.
No process may exit while holding resources.
Is bankers algo, a practical algorithm?
Review Questions
Ref: Timothy J Williams (MCQs
in Computer Sc.)
1. Concurrent processes are processes that
1. Do not overlap in time
2. Overlap in time
3. Are executed by the CPU at the same time
4. None of the above
2. The only state transition that is initiated by the user process itself is
1. Block
2. Dispatch
3. Wakeup
3. An OS contains 3 user processes each requiring 2 units of resource R. The minimum
number of units of R such that no deadlock will ever occur is
1. 3
2. 4
3. 5
4. 6
4. With a single resource, deadlock occurs
1. If there are more than two processes competing for that resource
2. If there are only two processes competing for that resource
3. If there is a single process competing for that resource
4. None of the above
5. At a particular time, the value of a counting semaphore is 10. It will become 7 after
1. 3 V operations
2. 3 P operations
3. 5 V operations and 2 P operations
4. 13 P operations and 10 V operations
1. A computer system has 6 tape drives, with n processes competing for them. Each
process may need 3 tape drives. The maximum value of n for which the system is
guaranteed to be deadlock free is
1. 2
2. 3
3. 4
4. 1
2. A state is safe if the system can allocate resources to each process (up to its maximum) in
some order and still avoid deadlock. Which of the following is true?
1. Deadlock state is unsafe
2. Unsafe state must lead to a deadlock
3. Deadlock state is a subset of unsafe state
3. Consider a system having m resources of the same type. These resources are shared by
3 processes A, B, and C, which have peak time demands of 3, 4, 6 respectively. The
minimum value of m that ensures that deadlock will never occur is
1. 12
2. 11
3. 13
4. 14
4. A system has 3 processes sharing 4 resources. If each process needs a maximum of 2
units then, deadlock
1. May occur
2. Can never occur
3. Has to occur
4. None of the above
1. At a particular time of computation, the value of a counting semaphore is 7. Then 20 P
operations and x V operations were completed on this semaphore. If the final value of the
semaphore is 5, x will be
1. 15
2. 22
3. 18
4. 13
2. Preemptive scheduling is the strategy of temporarily suspending a running process
1. Before the CPU time slice expires
2. To allow starving processes to run
3. When it requests I/O
4. None of the above
3. Which of the following is a service not supported by OS?
1. Protection
2. Accounting
3. Compilation
4. I/O operation
4. Which of the following scheduling algorithm is well suited for a time shared OS?
1. SJF
2. RR
3. FCFS
5. Which of the following is the most suitable scheduling scheme in a real-time OS
1. RR
2. FCFS
3. Pre-emptive scheduling
4. None of the above
1. In the following process state transition diagram for a uni-
processor system, assume that there are always some processes
in the ready state:

Now consider the following statements:


1. If a process makes a transition D, it would result in another process
making transition A immediately.
2. A process P2 in blocked state can make transition E while another
process P1 is in running state
3. The OS uses preemptive scheduling
4. The OS uses non-preemptive scheduling
Which of the above are true?
1. 1st and 2nd, 2. 1st and 3rd, 3. 2nd and 3rd, 4. 2nd and 4th
1. A single processor system has three resource types X, Y, and
Z, which are shared by three processes. There are five units
of each resource type. Consider the following scenario,
where the column alloc denotes resources allocated and
request denotes resourced needed to complete execution.
Which of these processes will finish LAST?

a. P0
b. P1
c. P2
d. None of the above since the system is in a deadlock.
1. Consider the resource allocation graph shown below.
Is it in a deadlocked state?
1. Yes
2. No
3. Impossible to
determine

2. In a Round Robin CPU scheduling algorithm, as the


time quantum is increased, the average turn around
time:
a. Increases
b. Decreases
c. Remains constant
d. Varies irregularly
1. Each process Pi, i=1, 2, 9 is coded as follows:

repeat
P(mutex)
{C-S}
V(mutex)
forever

The code for P10 is given as below:


repeat
V(mutex)
{C-S}
P(mutex)
forever
What is the largest number of processes that can be inside the C-S at
any moment?
a. 1
b. 2
c. 3
d. 4
e. None of the above
A) Consider the following statements about user level threads and
KLTs. Which one of the following statement is false?

1. Context switch time is longer for KLTs than for ULTs


2. User level threads do not need any hardware support
3. Related KLTs can be scheduled on different processors in a
multiprocessor system
4. Blocking one KLT blocks all related thread

B) Two processes P1, and P2 need to access CS of code. Consider


the following synchronization construct used by processes. wants1
and wants2 are shared variables initialized to false.

while (true) { while (true) {


wants1= true; wants2= true;
while (wants2 == true) ; while (wants1 == true) ;
// C-S // C-S
wants1= false; wants2= false;
} }
Which of the following statements is true about the above?
1. It does not ensure ME
2. It does not ensure bounded waiting
3. It does not prevent deadlock, but ensures Mutual Exclusion
1. The following program consists of 3 concurrent processes
and 3 binary semaphores. The semaphores are initialized as
S0=1, S1=0, and S2=0.

P1 P2

P0

How many times will process P0 print 0?


1. At least twice
2. Exactly twice
3. Exactly thrice
4. Exactly once
1. A system has four processes and five allocatable
resources. The current allocation and maximum needs are
as follows:

Allocated Maximum Available


A 10211 11213 00x11
B 20110 22210
C 11010 21310
D 11110 11221

What is the smallest value of x for which this is a safe state?


Next Class

Memory management

Potrebbero piacerti anche