0% found this document useful (0 votes)
20 views

Assignment 3 Os

The document explains the four necessary conditions for deadlock - mutual exclusion, hold and wait, no preemption, and circular wait. It then discusses resource allocation graphs and how they can be used to represent processes and resources in a system. Methods for handling deadlocks like deadlock prevention, avoidance and detection are also explained.

Uploaded by

Sahibjot Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Assignment 3 Os

The document explains the four necessary conditions for deadlock - mutual exclusion, hold and wait, no preemption, and circular wait. It then discusses resource allocation graphs and how they can be used to represent processes and resources in a system. Methods for handling deadlocks like deadlock prevention, avoidance and detection are also explained.

Uploaded by

Sahibjot Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 3

1. Explain the necessary conditions that may lead to a deadlock situation.

Ans - Necessary Conditions of Deadlock

There are four different conditions that result in Deadlock. These four conditions are also known
as Coffman conditions and these conditions are not mutually exclusive. Let's look at them one by
one.

 Mutual Exclusion: A resource can be held by only one process at a time. In other words, if
a process P1 is using some resource R at a particular instant of time, then some other
process P2 can't hold or use the same resource R at that particular instant of time. The
process P2 can make a request for that resource R but it can't use that resource
simultaneously with process P1.

 Hold and Wait: A process can hold a number of resources at a time and at the same time,
it can request for other resources that are being held by some other process. For example,
a process P1 can hold two resources R1 and R2 and at the same time, it can request some
resource R3 that is currently held by process P2.

 No preemption: A resource can't be preempted from the process by another process,


forcefully. For example, if a process P1 is using some resource R, then some other process
P2 can't forcefully take that resource. If it is so, then what's the need for various
scheduling algorithm. The process P2 can request for the resource R and can wait for that
resource to be freed by the process P1.
 Circular Wait: Circular wait is a condition when the first process is waiting for the resource
held by the second process, the second process is waiting for the resource held by the
third process, and so on. At last, the last process is waiting for the resource held by the
first process. So, every process is waiting for each other to release the resource and no
one is releasing their own resource. Everyone is waiting here for getting the resource. This
is called a circular wait.

2. Is it possible to have a deadlock involving only one single process? Explain your answer
Ans - It is not possible to have a deadlock involving only one single process. The deadlock involves a
circular “hold-and-wait” condition between two or more processes, so “one” process cannot hold a
resource, yet be waiting for another resource that it is holding.So for one process, deadlock can not
be possible . One process dissatisfy hold and wait condition for deadlock.

3. What are the various methods for handling deadlocks?

Ans - 1. Deadlock Ignorance

Deadlock Ignorance is the most widely used approach among all the mechanism. This is being used
by many operating systems mainly for end user uses. In this approach, the Operating system
assumes that deadlock never occurs. It simply ignores deadlock. This approach is best suitable for a
single end user system where User uses the system only for browsing and all other normal stuff.

There is always a tradeoff between Correctness and performance. The operating systems like
Windows and Linux mainly focus upon performance. However, the performance of the system
decreases if it uses deadlock handling mechanism all the time if deadlock happens 1 out of 100 times
then it is completely unnecessary to use the deadlock handling mechanism all the time.

In these types of systems, the user has to simply restart the computer in the case of deadlock.
Windows and Linux are mainly using this approach.

2. Deadlock prevention

Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and circular wait
holds simultaneously. If it is possible to violate one of the four conditions at any time then the
deadlock can never occur in the system.

The idea behind the approach is very simple that we have to fail one of the four conditions but there
can be a big argument on its physical implementation in the system.

3. Deadlock avoidance

In deadlock avoidance, the operating system checks whether the system is in safe state or in unsafe
state at every step which the operating system performs. The process continues until the system is
in safe state. Once the system moves to unsafe state, the OS has to backtrack one step.

In simple words, The OS reviews each allocation so that the allocation doesn't cause the deadlock in
the system.

4. Deadlock detection and recovery

This approach let the processes fall in deadlock and then periodically check whether deadlock occur
in the system or not. If it occurs then it applies some of the recovery methods to the system to get
rid of deadlock.

4. What is a resource allocation graph? How do you obtain a wait-for graph from it? Explain
their uses.
Ans - resource allocation graph is explained to us what is the state of the system in terms of
processes and resources. Like how many resources are available, how many are allocated and what
is the request of each process. Everything can be represented in terms of the diagram. One of the
advantages of having a diagram is, sometimes it is possible to see a deadlock directly by using RAG,
but then you might not be able to know that by looking at the table. But the tables are better if the
system contains lots of process and resource and Graph is better if the system contains less number
of process and resource.

We know that any graph contains vertices and edges. So RAG also contains vertices and edges. In
RAG vertices are two type –

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.

Now coming to the edges of RAG.There are two types of edges in RAG –

1. Assign Edge – If you already assign a resource to a process then it is called Assign edge.

2. Request Edge – It means in future the process might want some resource to complete the
execution, that is called request edge.

So, if a process is using a resource, an arrow is drawn from the resource node to the process node. If
a process is requesting a resource, an arrow is drawn from the process node to the resource node.

5. Consider the deadlock situation there may occur in the dining philosophers problem when the
philosophers obtain the chopsticks one at a time. Discuss how the four necessary conditions for
deadlock indeed hold in this setting. Discuss how deadlocks could be avoided by eliminating any
one of the four conditions.

Ans - When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks up
that chopstick. Then he waits for the right chopstick to be available, and then picks it too. After
eating, he puts both the chopsticks down.

But if all five philosophers are hungry simultaneously, and each of them pickup one chopstick, then a
deadlock situation occurs because they will be waiting for another chopstick forever. The possible
solutions for this are:
1. A philosopher must be allowed to pick up the chopsticks only if both the left and right chopsticks
are available.

2. Allow only four philosophers to sit at the table. That way, if all the four philosophers pick up four
chopsticks, there will be one chopstick left on the table. So, one philosopher can start eating and
eventually, two chopsticks will be available. In this way, deadlocks can be avoided.

6. Consider a system consisting of 4 resources of the same type that are shared by 3 processes,
each of which needs at most 2 resources. Show that the system is deadlock free.

Ans - Suppose the system is deadlocked. This implies that each process is holding one resource and is
waiting for one more. Since there are three processes and four resources, on process must be able
to obtain two resources. This process requires no more resources and therefore it will return its
resources when done.

7.Prove that the safety algorithm (Banker’s algorithm) requires an order of m x n2 operations.

Ans- Code that implement safety algorithm:-

for (int i = 0; i < n; i++) {

// first find a thread that can finish

for (int j = 0; j < n; j++) {

if (!finish[j]) {

boolean temp = true;

for (int k = 0; k < m; k++) {

if (need[j][k] > work[k])

temp = false;

if (temp) { // if this thread can finish

finish[j] = true;

for (int x = 0; x < m; x++)

work[x] += work[j][x];

} } } }.

As can be seen, the nested outer loops—both of which loop through n times- provide the n2
performance. Within these outer loops are sequential inner loops which loop m times. The big-oh of
this algorithm is O(m x n2) therefore safety algorithm (Banker’s algorithm) requires an order of m x
n2 operations.

8. Consider the following snapshot of a systexitm:

Allocation Max Available


AB C D ABCD AB C D

P0 0012 0012 1520

P1 1000 1750

P2 1354 2356

P3 0632 0652

P4 0014 0656

Answer the following questions using the banker’s algorithm:

a. What is the content of the matrix Need?

b. Is the system in a safe state?

c. If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately?

Ans -

a. The values of Need for processes P0 through P4 respectively are (0, 0, 0, 0), (0, 7, 5, 0), (1, 0, 0, 2),
(0, 0, 2, 0), and (0, 6, 4, 2).

b. Yes. With Available being equal to (1, 5, 2, 0), either process P0 or P3 could run. Once process P3
runs, it releases its resources, which allow all other existing processes to run.

c. The request can be granted immediately. This results in the value of Available being (1, 1, 0, 0).
One ordering of processes that can finish is P0, P2, P3, P1, and P4.

9. Can a system detect that some of its processes are starving? If you answer “yes,” explain how it
can. If you answer “no,” explain how the system can deal with the starvation problem.

Ans –

we will define starvation as the situation whereby a process must wait beyond a reasonable period
of time—perhaps indefinitely—before receiving a requested resource. One way of detecting
starvation would be to first identify a period of time—T — that is considered unreasonable. When a
process requests a resource, a timer is started. If the elapsed time exceeds T, then the process is
considered to be starved. One strategy for dealing with starvation would be to adopt a policy where
resources are assigned only to the process that has been waiting the longest. For example, if process
Pa has been waiting longer for resource X than process Pb , the request from process Pb would be
deferred until process Pa ’s request has been satisfied. Another strategy would be less strict than
what was just mentioned. In this scenario, a resource might be granted to a process that has waited
less than another process, providing that the other process is not starving. However, if another
process is considered to be starving, its request would be satisfied first.

10. What do you mean by critical section problem? Explain with suitable example.
Ans - critical section problems is a set of instructions that must be controlled so as to allow exclusive
access to one process access to the critical section is limited to n processes instead of one process
execution of the critical section by processes is mutually exclusive in time

for example,

repeat

entry section

critical section

exit section

remainder section

until FALSE

11. What is the meaning of the term busy waiting? What other kinds of waiting are there in an
operating system? Can busy waiting be avoided altogether? Explain your answer.

Ans - Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop
without relinquishing the processor. Alternatively, a process could wait by relinquishing the
processor, and block on a condition and wait to be awakened at some appropriate time in the
future. Busy waiting can be avoided but incurs the overhead associated with putting a process to
sleep and having to wake it up when the appropriate program state is reached.

12.Explain deadlock prevention and deadlock recovery?

Ans – Deadlock Prevention

We can prevent Deadlock by eliminating any of the four conditions.

1. Mutual Exclusion

2. Hold and Wait

3. No preemption

4. Circular wait

Deadlock Recovery

A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is time and
space consuming process. Real-time operating systems use Deadlock recovery.

Recovery method

1. Killing the process: killing all the process involved in the deadlock. Killing process one by one. After
killing each process check for deadlock again keep repeating the process till system recover from
deadlock.

2. Resource Preemption: Resources are preempted from the processes involved in the deadlock,
preempted resources are allocated to other processes so that there is a possibility of recovering the
system from deadlock. In this case, the system goes into starvation.

You might also like