0% found this document useful (0 votes)
101 views6 pages

OS Assignment: Deadlocks

The document discusses the dining philosophers problem and its solution using semaphores. It describes the problem as K philosophers sharing a table and K chopsticks, where each philosopher needs two chopsticks to eat and putting one philosopher down can cause a deadlock. The proposed solution uses semaphores to represent each chopstick, acquiring and releasing them to allow philosophers to eat without deadlock.

Uploaded by

Abhinav arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views6 pages

OS Assignment: Deadlocks

The document discusses the dining philosophers problem and its solution using semaphores. It describes the problem as K philosophers sharing a table and K chopsticks, where each philosopher needs two chopsticks to eat and putting one philosopher down can cause a deadlock. The proposed solution uses semaphores to represent each chopstick, acquiring and releasing them to allow philosophers to eat without deadlock.

Uploaded by

Abhinav arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OS Assignment: Deadlocks

Q1) State dining philosopher’s problem and give a solution using semaphores. Write
structure of philosopher.
Ans. The Dining Philosopher Problem – The dining philosophers problem states that there are 5(or K)
philosophers sharing a circular table and they eat and think alternatively. There is a bowl of rice for each of the
philosophers and 5 chopsticks. A philosopher needs both their right and left chopstick to eat. A hungry
philosopher may only eat if there are both chopsticks available. Otherwise a philosopher puts down their
chopstick and begin thinking again.
The dining philosopher is a classic synchronization problem as it demonstrates a large class of concurrency
control problems.

Solution of Dining Philosophers Problem:


A solution of the Dining Philosophers Problem is to use a semaphore to represent a chopstick. A chopstick can
be picked up by executing a wait operation on the semaphore and released by executing a signal semaphore.
The structure of the chopstick is shown below:
semaphore chopstick [5];
Initially the elements of the chopstick are initialized to 1 as the chopsticks are on the table and not picked up by
a philosopher.
The structure of a random philosopher i is given as follows:
do {
wait( chopstick[i] );
wait( chopstick[ (i+1) % 5] );
EATING THE RICE
signal( chopstick[i] );
signal( chopstick[ (i+1) % 5] );
THINKING
} while(1);
In the above structure, first wait operation is performed on chopstick[i] and chopstick [ (i+1) % 5]. This means
that the philosopher i has picked up the chopsticks on his sides. Then the eating function is performed.
After that, signal operation is performed on chopstick[i] and chopstick [ (i+1) % 5]. This means that the
philosopher i has eaten and put down the chopsticks on his sides. Then the philosopher goes back to thinking.
Structure of Philosopher:
There are some Philosophers whose work is just thinking and eating. Let there are 5 (for example) philosophers.
They sat at a round table for dinner. To complete dinner each must need two Forks (spoons). But there are only
5 Forks available (Forks always equal to no. of Philosophers) on table. They take in such a manner that, first
take left Fork and next right Fork. But problem is they try to take at same time. Since they are trying at same
time, Fork 1, 2, 3, 4, 5 taken by Philosopher 1, 2, 3, 4, 5 respectively (since they are left side of each). And each
one tries to ta ke right side Fork. But no one found available Fork. And also, that each one thinks that someone
will release the Fork and then I can eat. This continuous waiting leads to Dead Lock situation.
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
Dining Arrangement:

Solution: This solution to the problem is the one originally proposed by Dijkstra. It assigns a partial order to
the resources (the forks, in this case), and establishes the convention that all resources will be requested in order,
and that no two resources unrelated by order will ever be used by a single unit of work at the same time. Here,
the resources (forks) will be numbered 1 through 5 and each unit of work (philosopher) will always pick up the
lower-numbered fork first, and then the higher-numbered fork, from among the two forks they plan to use. The
order in which each philosopher puts down the forks does not matter. In this case, if four of the five philosophers
simultaneously pick up their lower-numbered fork, only the highest-numbered fork will remain on the table, so
the fifth philosopher will not be able to pick up any fork. Moreover, only one philosopher will have access to
that highest-numbered fork, so they will be able to eat using two forks.

While the resource hierarchy solution avoids deadlocks, it is not always practical, especially when the list of
required resources is not completely known in advance. For example, if a unit of work holds resources 3 and 5
and then determines it needs resource 2, it must release 5, then 3 before acquiring 2, and then it must re-acquire
3 and 5 in that order. Computer programs that access large numbers of database records would not run efficiently
if they were required to release all higher-numbered records before accessing a new record, making the method
impractical for that purpose.
Q2) Why is deadlock state more critical than starvation? Describe resource allocation
graph with a deadlock, with a cycle but no deadlock.
Ans. Starvation occurs when one or more threads in your program are blocked from gaining access to a resource
and, as a result, cannot make progress. Deadlock, the ultimate form of starvation, occurs when two
or more threads are waiting on a condition that cannot be satisfied.
As Banker’s algorithm using some kind of table like allocation, request, available all that thing to understand
what is the state of the system. Similarly, if you want to understand the state of the system instead of using those
table, actually tables are very easy to represent and understand it, but then still you could even represent the
same information in the graph. That graph is called Resource Allocation Graph (RAG). So, 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.
Example 1 (Single instances RAG) –

If there is a cycle in the Resource Allocation Graph and each resource in the cycle provides only one instance,
then the processes will be in deadlock. For example, if process P1 holds resource R1, process P2 holds resource
R2 and process P1 is waiting for R2 and process P2 is waiting for R1, then process P1 and process P2 will be
in deadlock.

Here’s another example, that shows Processes P1 and P2 acquiring resources R1 and R2 while process P3 is
waiting to acquire both resources. In this example, there is no deadlock because there is no circular dependency.
So cycle in single-instance resource type is the sufficient condition for deadlock.
Q3) What is deadlock? Explain the necessary conditions for its occurrence.

Ans. Deadlock is a situation where a set of processes are blocked because each process is holding a resource
and waiting for another resource acquired by some other process.

Consider an example when two trains are coming toward each other on same track and there is only one track,
none of the trains can move once they are in front of each other. Similar situation occurs in operating systems
when there are two or more processes hold some resources and wait for resources held by other(s).
Conditions For Deadlock-

1. Mutual Exclusion-
By this condition,

 There must exist at least one resource in the system which can be used by only one process at a time.
 If there exists no such resource, then deadlock will never occur.
 Printer is an example of a resource that can be used by only one process at a time.

2. Hold and Wait-


By this condition,

 There must exist a process which holds some resource and waits for another resource held by some other
process.

3. No Pre-emption-
By this condition,
 Once the resource has been allocated to the process, it can not be preempted.
 It means resource cannot be snatched forcefully from one process and given to the other process.
 The process must release the resource voluntarily by itself.

4. Circular Wait-
By this condition,
 All the processes must wait for the resource in a cyclic manner where the last process waits for the resource
held by the first process.

Here,
 Process P1 waits for a resource held by process P2.
 Process P2 waits for a resource held by process P3.
 Process P3 waits for a resource held by process P4.
 Process P4 waits for a resource held by process P1.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy