Chapter6-HE180347 SE1918

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1. Give at least 2 examples of Deadlocks?

For example, many offices have a local area network with


many computers connected to it. Often devices such as
scanners, Blu-ray/DVD recorders, printers, and tape drives are
connected to the network as shared resources, available to
any user on any machine. If these devices can be reserved
remotely (i.e., from the user’s home machine), deadlocks of the
same kind can occur as described above. More complicated
situations can cause deadlocks involving three, four, or more
devices and user

Scenario: Two processes, P1P_1P1​and P2P_2P2​, need two


resources, R1R_1R1​and R2R_2R2​, to complete their tasks.
The resources are limited and can only be held by one process
at a time.

1. Process P1P_1P1​holds R1R_1R1​and requests R2R_2R2​.


2. Process P2P_2P2​holds R2R_2R2​and requests R1R_1R1​.

Neither process can proceed because each is waiting for a


resource held by the other, resulting in a deadlock.

2. What are preemtable and non-preemptable resources?

-A preemptable resource :

+ A preemptable resource is one that can be taken away from


the process owning it with no ill effects. Memory is an
example of a preemptable resource.

+ Example : A system with 1 GB of user memory, one printer,


and two 1-GB processes that each want to print something.
Process A requests and gets the printer, then starts to
compute the values to print. Before it has finished the
computation, it exceeds its time quantum and is swapped
out to disk. Process B now runs and tries, unsuccessfully
as it turns out, to acquire the printer. Potentially, we now
have a deadlock situation, because A has the printer and B
has the memory, and neither one can proceed without the
resource held by the other. Fortunately, it is possible to
preempt (take away) the memory from B by swapping it out
and swapping A in. Now A can run, do its printing, and then
release the printer. No deadlock occurs.

-A non - preemptable resource :


+ A non - preemptable resource is one that cannot be taken
away from its current owner without potentially causing
failure.

+ Example : If a process has begun to burn a Blu-ray,


suddenly taking the Blu-ray recorder away from it and
giving it to another process will result in a garbled Blu-ray.
Blu-ray recorders are not pre- emptable at an arbitrary
moment. Whether a resource is preemptible depends on the
context. On a standard PC, memory is preemptible because
pages can always be swapped out to disk to recover it.
However, on a smartphone that does not support swapping
or paging, deadlocks cannot be avoided by just swapping
out a memory hog.

3. Give the definition of Deadlock.

A deadlock is a specific condition in computer science where a


set of processes are unable to proceed because each process
is waiting for an event that can only be triggered by another
waiting process in the set. Deadlocks are a critical issue in
concurrent programming and operating systems, leading to a
standstill in system operations.

Detailed Explanation of Deadlock:

1. Necessary Conditions for Deadlock: For a deadlock to


occur, four conditions must be present simultaneously:
○ Mutual Exclusion: At least one resource must be
held in a non-shareable mode. Only one process can
use the resource at any given time.
○ Hold and Wait: A process is currently holding at
least one resource and requesting additional
resources that are being held by other processes.
○ No Preemption: Resources cannot be forcibly taken
away from the processes holding them until the
resource is released voluntarily by the process.
○ Circular Wait: A set of processes are waiting for
each other in a circular chain. For instance, process
P1 is waiting for a resource held by process P2,
process P2 is waiting for a resource held by process
P3, and process P3 is waiting for a resource held by
process P1.
2. Example Scenario:
○ Imagine two processes, P1 and P2, and two
resources, R1 and R2.
■ P1 has acquired R1 and needs R2 to proceed.
■ P2 has acquired R2 and needs R1 to proceed.
○ Both processes end up waiting indefinitely for the
other to release the required resource, leading to a
deadlock.
3. Consequences of Deadlock:
○ Resource Wastage: Resources held by deadlocked
processes are not utilized efficiently.
○ System Halt: A part of the system or the entire
system may halt, as the deadlocked processes are
unable to proceed.
○ Performance Degradation: The overall performance
of the system degrades due to blocked processes
and resource underutilization.
4. Deadlock Prevention, Avoidance, and Detection:
○ Prevention: Involves designing the system in such a
way that it structurally negates one of the necessary
conditions for deadlock.
■ Ensure mutual exclusion is not required.
■ Do not allow hold and wait by requiring
processes to request all needed resources at
once.
■ Allow preemption of resources.
■ Avoid circular wait by imposing a total ordering
of resource types and ensuring that processes
request resources in increasing order of
enumeration.
○ Avoidance: Dynamically examines the resource
allocation state to ensure that a circular wait
condition can never exist.
■ Banker’s Algorithm is a classic example used
for deadlock avoidance.
○ Detection and Recovery: Periodically checks the
system for deadlocks and takes action to recover
from the deadlock if detected.
■ Use of resource allocation graphs and various
algorithms to detect cycles in these graphs.
■ Recovery can be done by terminating one or
more processes or preempting resources from
some processes.

Deadlocks are an important concept in operating systems, and


understanding them is crucial for designing systems that can
either prevent or efficiently handle such conditions

4. Explain how deadlock occurs and how it can be avoided


using Fig. 6.4.
- Explain how deadlock occurs :

+ The requests and releases of the three processes are


given in Fig. 6-4(a)-(c). The operating system is free to
run any unblocked process at any instant, so it could
decide to run A until A finished all its work, then run B to
completion, and finally run C.
+ This ordering does not lead to any deadlocks (because
there is no competition for resources) but it also has no
parallelism at all. In addition to requesting and releasing
resources, processes compute and do I/O. When the
processes are run se- quentially, there is no possibility
that while one process is waiting for I/O, another can use
the CPU. Thus, running the processes strictly
sequentially may not be optimal. On the other hand, if
none of the processes does any I/O at all, shortest job
first is better than round robin, so under some
circumstances running all proe- esses sequentially may
be the best way.

+ The processes do both I/O and computing, so that round


robin is a reasonable scheduling algorithm. The
resource requests might oc- cur in the order of Fig.
6-4(d). If these six requests are carried out in that order,
the six resulting resource graphs are shown in Fig.
6-4(e)-(j). After request 4 has been made, A blocks
waiting for S, as shown in Fig. 6-4(h). In the next two
steps B and C also block, ultimately leading to a cycle
and the deadlock of Fig. 6-4(j).

+ However, the operating system is not required to run the


processes in any special order. In particular, if granting a
particular re- quest might lead to deadlock, the operating
system can simply suspend the process without
granting the request (i.e., just not schedule the process)
until it is safe. In Fig. 6-4, if the operating system knew
about the impending deadlock, it could sus- pend B
instead of granting it S. By running only A and C, we
would get the re- quests and releases of Fig. 6-4(k)
instead of Fig. 6-4(d). This sequence leads to the
resource graphs of Fig. 6-4(1)-(q), which do not lead to
deadlock.
+ After step (q), process B can be granted S because A is
finished and Chas everything it needs. Even if B blocks
when requesting T, no deadlock can occur. B will just
wait until C is finished.

- Four strategies are used for dealing with deadlocks :

1. Just ignore the problem. Maybe if you ignore it, it will


ignore you.

2. Detection and recovery. Let them occur, detect them,


and take action.

3. Dynamic avoidance by careful resource allocation.

4. Prevention, by structurally negating one of the four


conditions.

5. Explain the Ostrich Algorithm.

- The simplest approach is the ostrich algorithm: stick your head in the
sand and pretend there is no problem.

- People react to this strategy in different ways. Math - ematicians find it


unacceptable and say that deadlocks must be prevented at all costs.
Engineers ask how often the problem is expected, how often the system
crashes for other reasons, and how serious a deadlock is. If deadlocks
occur on the average once every five years, but system crashes due to
hardware failures and op- erating system bugs occur once a week, most
engineers would not be willing to pay a large penalty in performance or
convenience to eliminate deadlocks.

- To make this contrast more specific, consider an operating system that


blocks the caller when an open system call on a physical device such as
a Blu-ray driver or a printer cannot be carried out because the device is
busy. Typically it is up to the device driver to decide what action to take
under such circumstances. Blocking or returning an error code are two
obvious possibilities. If one process suc- cessfully opens the Blu-ray
drive and another successfully opens the printer and then each process
tries to open the other one and blocks trying, we have a dead- lock. Few
current systems will detect this.

6. Explain and Give example of Deadlock Detection with One


Resource of Each Type.
- Explain :
+ Such a system might have one scanner, one Blu-ray
recorder, one plotter, and one tape drive, but no more than
one of each class of resource.

+ For such a system, we can construct a resource graph. If this


graph contains one or more cycles, a deadlock exists. Any
process that is part of a cycle is deadlocked. If no cycles
exist, the system is not deadlocked.

+ Although it is relatively simple to pick out the deadlocked


processes by visual inspection from a simple graph, for use
in actual systems we need a formal algorithm for detecting
deadlocks.

- Example :
As an example of a system more complex than those we have
looked at so far, consider a system with seven processes, A
through G, and six resources, R through W.

1. Process A holds R and wants S.

2. Process B holds nothing but wants T

3. Process C holds nothing but wants S.

4. Process D holds U and wants S and T

5. Process E holds T and wants V

6. Process F holds Wand wants S

7. Process G holds V and wants U


+ We can construct the resource graph of Fig 6-5(a). This
graph contains one cycle, which can be seen by visual
inspection.

+ The cycle is shown in Fig. 6-5(b), From this cycle, we


can see that processes D, E, and Gate all deadlocked
Processes A, C, and F are not deadlocked because S
can be allocated to any one of them, which then
finishes and returns it. Then the other two can take it in
turn and also complete it.

7. Explain and Give example of Deadlock Detection with


Multiple Resources of Each

- Explain :

+ The deadlock detection algorithm is based on comparing vectors.


+ Each process is initially said to be unmarked. As the algorithm
progresses, processes will be marked, indicating that they are able
to complete and are thus not deadlocked. When the algorithm
terminates, any unmarked processes are known to be deadlocked.
This algorithm assumes a worst-case scenario: all processes keep
all acquired resources until they exit.

+ The deadlock detection algorithm can now be given as follows.


1. Look for an unmarked process, P, for which the ith
row of R is less than or equal to A.
2. If such a process is found, add the ith row of C to A,
mark the process, and go back to step 1
3. If no such process exists, the algorithm terminates.

+ When the algorithm finishes, all the unmarked processes, if any,


are deadlocked. What the algorithm is doing in step 1 is looking for
a process that can be run to completion. Such a process is
characterized as having resource demands that can be met by the
currently available resources. The selected process is then run
until it finishes, at which time it returns the resources it is holding
to the pool of avail- able resources. It is then marked as completed.
If all the processes are ultimately able to run to completion, none
of them are deadlocked. If some of them can never finish, they are
deadlocked. Although the algorithm is nondeterministic (because
it may run the processes in any feasible order), the result is always
the same.

- Example

+ Here we have three processes and four resource classes, which


we have arbitrarily labeled tape drives, plotters, scanners, and
Blu-ray drives. Process I has one scanner. Process 2 has two tape
drives and a Blu-ray drive. Process 3 has a plotter and two
scanners. Each process needs additional resources, as shown by
the R matrix.

+ To run the deadlock detection algorithm, we look for a process


whose resource request can be satisfied. The first one cannot be
satisfied because there is no Blu- ray drive available. The second
cannot be satisfied either, because there is no scan- ner free.
Fortunately, the third one can be satisfied, so process 3 runs and
eventual- ly returns all its resources, giving A=(2220)

+ Now the remaining process can run. There is no deadlock in the


system. Now consider a minor variation of the situation of Fig. 6-7.
Suppose that proe- ess 3 needs a Blu-ray drive as well as the two
tape drives and the plotter. None of the requests can be satisfied,
so the entire system will eventually be deadlocked. Even if we give
process 3 its two tape drives and one plotter, the system
deadlocks when it requests the Blu-ray drive

You might also like