Unit 2 Answers
Unit 2 Answers
Unit 2 Answers
Unit 2
It is a situation where each of the computer process waits for a resource which is being assigned to
some another process. In this situation, none of the process gets executed since the resource it needs, is
held by some other process which is also waiting for some other resource to be released.
1) Mutual Exclusion:
A resource can only be shared in mutually exclusive manner. It implies, if two process cannot
use the same resource at the same time.
2) Hold and Wait:
A process waits for some resources while holding another resource at the same time.
3) No preemption:
The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.
4) Circular Wait:
All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.
Stick your head in the sand and pretend there is no problem at all, this method of solving any problem is
called Ostrich Algorithm
In General :
System failure
Compiler error
programming bugs
hardware crashes
Wait & Try
B)How deadlocks can be detected in single instance and multiple instance resources? Explain
with an example. (6 mark)
Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite amount of time
Let n
= number of processes, and m = number of resources types.
Rj
Safety Algorithm
1. Let Work and Finish be vectors of length m and n,respectively.
2. Initialize: Work = Available
Finish [i] = false fori = 0, 1, …,n- 1
3. Find an isuch that both:
(a) Finish [i] = false
(b) Needi=Work
If no such iexists, go to step 4
4. Work = Work +
Allocationi Finish[i] = truego to step 2
5. IfFinish [i] == true for all i, then the system is in a safe state
6. Resource-Request Algorithm for Process Pi
Request = request vector for process Pi. If Requesti[j] = k then process Pi wants
The system is in a safe state since the sequence <P1, P3, P4, P2, P0>
satisfies safety criteria P1 Request (1,0,2)
Check that Request £ Available (that is, (1,0,2) £ (3,3,2) true
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement
Deadlock is a situation where a process or a set of processes is blocked, waiting for some
other resource that is held by some other waiting process. It is an undesirable state of the
system. The following are the four conditions that must hold simultaneously for a
deadlock to occur.
1. Mutual Exclusion – A resource can be used by only one process at a time. If
another process requests for that resource then the requesting process must be
delayed until the resource has been released.
2. Hold and wait – Some processes must be holding some resources in the non-
shareable mode and at the same time must be waiting to acquire some more
resources, which are currently held by other processes in the non-shareable
mode.
3. No pre-emption – Resources granted to a process can be released back to the
system only as a result of voluntary action of that process after the process has
completed its task.
4. Circular wait – Deadlocked processes are involved in a circular chain such that
each process holds one or more resources being requested by the next process in
the chain.
Methods of handling deadlocks: There are four approaches to dealing with deadlocks.
1. Deadlock Prevention
2. Deadlock avoidance (Banker's Algorithm)
The OS can detect the deadlocks with the help of Resource allocation graph.
The OS can detect the deadlocks with the help of Resource allocation graph.
In single instanced resource types, if a cycle is being formed in the system then there will
definitely be a deadlock.
Multiple instanced resource type graph, detecting a cycle is not just enough. We have to apply the
safety algorithm on the system by converting the resource allocation graph into the allocation
matrix and request matrix.
1. Process vertex – Every process will be represented as a process vertex. Generally, the process will be
represented with a circle.
2. Resource vertex – Every resource will be represented as a resource vertex. It is also two type –
• Single instance type resource – It represents as a box, inside the box, there will be one dot. So the
number of dots indicate how many instances are present of each resource type.
• Multi-resource instance type resource – It also represents as a box, inside the box, there will be many
dots present.
10. Using Banker’s Algorithm, assume that there are 5 processes, P0 through P4, and 4 types of
resources. At T0 we have the following system state:
A B C D A B C D A B C D
P1 0 1 1 0 0 2 1 0 1 5 2 0
P2 1 2 3 1 1 6 5 2
P3 1 3 6 5 2 3 6 6
P4 0 6 3 2 0 6 5 2
P5 0 0 1 4 0 6 5 6
Banker’s Algorithm
Multiple instances
Each process must a priori claim maximum use
When a process requests a resource it may have to wait
When a process gets all its resources it must return them in a finite
amount of time Let n
= number of processes, and m = number of resources types.
Available: Vector of length m. If available [j] = k, there are k instances of
resource type
Rjavailable
Max: n x m matrix. If Max [i,j] = k, then process Pimay request at most k
Rj
Safety Algorithm
7. Let Work and Finish be vectors of length m and n,respectively.
8. Initialize: Work = Available
Finish [i] = false fori = 0, 1, …,n- 1
9. Find an isuch that both:
(c) Finish [i] = false
(d) Needi=Work
If no such iexists, go to step 4
10. Work = Work
+ Allocationi
Finish[i] = truego to step 2
The system is in a safe state since the sequence <P1, P3, P4, P2, P0>
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement