Os - Unit Iv 1 10
Os - Unit Iv 1 10
Resource:
A resource can be a piece of hardware such as tape drive, disk drive ,printer etc. or a piece of information such as a file ,a record
within a file ,a shared variable ,a critical section etc.
Resources come in two types:
• Preemptable •
Nonpreemptable.
A Preemptable resource is one which can be allocated to a given process for a period of time, then be allocated to another
process and then be reallocated to the first process without any ill effects. Examples of preemptable resources include memory,
buffers, CPU, array processor, etc.
A Nonpreemptable resource cannot be taken from one process and given to another without side effects. One obvious
example is a printer: certainly we would not want to take the printer away from one process and give it to another in the middle
of a print job.
Under the normal mode of operation, a process may utilize a resource in only the following sequence:
• Request. The process requests the resource. If the request cannot be granted immediately (for example, if the resource is
being used by another process), then the requesting process must wait until it can acquire the resource.
• Use. The process can operate on the resource (for example, if the resource is a printer, the process can print on the printer).
Deadlock:
• In a multiprogramming environment, several processes may compete for a finite number of resources.
• A process requests resources; if the resources are not available at that time, the process enters a waiting state.
Sometimes, a waiting process is never again able to change state, because the resources it has requested are
held by other waiting processes. This situation is called a deadlock.
Deadlock Characterization:
In a deadlock, processes never finish executing, and system resources are tied up, preventing other jobs from starting.
• Necessary Conditions:
• A deadlock situation can arise if the following four conditions are true
• 1.Mutual Exclusion
• 2. Hold and wait
• 3.No preemption • 4. Circular wait.
• Mutual exclusion : At least one resource must be held in a non-sharable mode; that is, only one process at a time can use
the resource. If another process requests that resource, the requesting process must be delayed until the resource has
been released.
• No preemption : Resources cannot be preempted; that is, a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
• Hold and wait: A process must be holding at least one resource and waiting to acquire additional resources that are
currently being held by other processes.
• Circular wait: A set {P0, P1, ..., Pn} of waiting processes must exist such that P0 is waiting for a resource held by P1, P1 is
waiting for a resource held by P2, ..., Pn−1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.
→Resources are denoted using rectangle. Since resource type Rj may have more than one instance, we
represent each such instance as a dot within the rectangle.
The sets P, R, and E: ◦ P = {P1, P2, P3}
◦ R = {R1, R2, R3, R4}
◦ E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3} Resource instances:
◦ One instance of resource type R1
◦ Two instances of resource type R2
◦ One instance of resource type R3 ◦ Three instances of resource type R4 Process states:
◦ Process P1 is holding an instance of resource type R2 and is waiting for an instance of
resource type R1.
◦ Process P2 is holding an instance of R1 and an instance of R2 and is waiting for
an instance of R3. Resource Allocation Graph
◦ Process P3 is holding an instance of R3.
1) To ensure that deadlocks never occur, the system can use either a deadlock prevention or a deadlock
avoidance scheme.
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot
hold:
1. Mutual Exclusion
2. Hold and wait 3.No preemption
4.Circular wait.
Deadlock avoidance requires that the operating system be given additional information in advance concerning
which resources a process will request and use during its lifetime. With this additional knowledge, the operating
system can decide for each request whether or not the process should wait.
2) If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then a deadlock
situation may arise.
– In this environment, the system can provide an algorithm that examines the state of the system to
determine whether a deadlock has occurred and an algorithm to recover from the deadlock (if a
deadlock has indeed occurred).
– In this case, the undetected deadlock will cause the system’s performance to deteriorate, because
resources are being held by processes that cannot run and because more and more processes, as
they make requests for resources, will enter a deadlocked state.
Eventually, the system will stop functioning and will need to be restarted manually.
Deadlock Prevention:
For a deadlock to occur, each of the four necessary conditions must hold.
• 1.Mutual Exclusion
• 2. Hold and wait
• 3.No preemption • 4.Circular wait.
Mutual Exclusion
• The mutual exclusion condition must hold for at least one resource must be non-sharable.
• Example: A printer cannot be simultaneously shared by several process
• Sharable resources, in contrast, do not require mutually exclusive access and thus cannot
be involved in a deadlock.
• Example :Read-only files are a good example of a sharable resource. If several processes
attempt to open a read-only file at the same time, they can be granted simultaneous access
to the file.
No Preemption:
To ensure that this condition does not hold, we can use the following protocol.
• If a process is holding some resources and requests another resource that cannot be immediately allocated
to it, then all resources the process is currently holding are preempted.
• Alternatively, if a process requests some resources, we first check whether they are available. If they are,
we allocate them. If they are not, we check whether they are allocated to some other process that is waiting
for additional resources. If so, we preempt the desired resources from the waiting process and allocate
them to the requesting process.
• Example: This protocol is often applied to resources whose state can be easily saved and restored later,
such as CPU registers and memory space. It cannot generally be applied to such resources as printers and
tape drivers
Circular Wait:
Way to ensure that this condition never holds is to impose a total ordering of all resource types and to require that
each process requests resources in an increasing order of enumeration.
• Example: Suppose Process P1 is allocated Resources R5.Now if P1 requests for Resources R4 and R3(which
are lesser than R5) such requests will not be granted. Only request for resource greater than R5 will be
granted
Developing an ordering, or hierarchy, in itself does not prevent deadlock. It is up to application developers to write
programs that follow the ordering
Deadlock Avoidance
• An alternative method for avoiding deadlocks is to require additional information about how resources are
to be requested.
• With this knowledge of the complete sequence of requests and releases for each process, the system can
decide for each request whether or not the process should wait in order to avoid a possible future deadlock.
Safe State:
• A state is safe if the system can allocate resources to each process (up to its maximum) in some order and
still avoid a deadlock.
• A system is in a safe state only if there exists a safe sequence.
• A safe state is not a deadlocked state. Conversely, a deadlocked state is an unsafe state. Not all unsafe states
are deadlocks.
• An unsafe state may lead to a deadlock. As long as the state is safe, the operating system can avoid unsafe
(and deadlocked) states.
• Example:
Safe, unsafe, and
• We consider a system with twelve magnetic tape drives and three processes: P0, P1,
deadlocked and
state P2.
spaces.
Unsafe state.
Resource-Allocation-Graph Algorithm :
Claim Edge:
• A claim edge Pi → Rj indicates that process Pi may request resource Rj at some time in the future.
• It resembles a request edge in direction but is represented in the graph by a dashed line
• When process Pi requests resource Rj , the claim edge Pi → Rj is converted to a request edge.
• Similarly, when a resource Rj is released by Pi , the assignment edge Rj → Pi is reconverted to a claim edge
Pi → Rj .
The resources must be claimed a priori in the system.
• If no cycle exists, then the allocation of the resource will leave the system in a safe state. • If a cycle is found,
then the allocation will put the system in an unsafe state.
• We must determine whether this new system state is safe. To do so, we again execute Safety algorithm on the
above data structures.
Deadlock Detection:
For Single Instance of Each Resource Type:
• Single Instance of Each Resource Type.
• If all resources have only a single instance, then we can define a deadlock detection algorithm that uses a
variant of the resource-allocation graph, called a wait-for graph.
• We obtain this graph from the resource-allocation graph by removing the resource nodes and collapsing the
appropriate edges.
Solution:
• In this, Work = [0, 0, 0] &
Finish = [false, false, false, false, false]
Ostrich Algorithm
• Deadlock Ignorance is also called as Ostrich Algorithm
• This algorithm is a well-used method to ignore problems. It gets its name
from The Ostrich and the way it sticks its head in the sand and ignores
everything that’s happening around just as the picture below shows
• Scientists all over the world believe that the most efficient method to deal
with deadlock is deadlock prevention.
• But the Engineers that deal with the system believe that deadlock prevention should be paid less attention
as there are very less chances for deadlock occurrence.
• System failure, compiler error, programming bugs, hardware crashes that occur once a week should be paid
more attention rather than deadlock problem that occur once in years.
• Therefore most of the engineers don’t pay much amount in eliminating the deadlock.
Pros of ignorance in deadlocks?
• No manpower is required to do any extra work. When the system crashes, just restart it. This is viable only
when a deadlock occurs rarely. If a computer is having deadlock once per 10 years then rebooting the
system is more preferred than pre Cons of ignorance in deadlocks?
• If it happens quite often, then this is not an optimal solution. You’ll have to invest some time in properly
dealing with deadlocks, that’s any of the next three methods mentioned above in prevention of deadlock.