Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment

Q1. state dining philosopher's problem and give a solution using semaphores, write
structure of philosopher.

Answers:

The Dining Philosopher Problem – The Dining Philosopher Problem states that K
philosophers seated around a circular table with one chopstick between each pair of
philosophers. There is one chopstick between each philosopher. A philosopher may eat if
he can pickup the two chopsticks adjacent to him. One chopstick may be picked up by any
one of its adjacent followers but not both.

Structure of philosopher:

Each philosopher is represented by the following pseudocode:

process P[i]
while true do
{
THINK;
PICKUP(CHOPSTICK[i], CHOPSTICK[i+1 mod 5]);
12 – ADARSH SINGH DIKHIT 1
EAT;
PUTDOWN(CHOPSTICK[i], CHOPSTICK[i+1 mod 5])
}

There are three states of philosopher : THINKING, HUNGRY and EATING. Here there are
two semaphores : Mutex and a semaphore array for the philosophers. Mutex is used such
that no two philosophers may access the pickup or putdown at the same time. The array is
used to control the behavior of each philosopher. But, semaphores can result in deadlock
due to programming errors.

Q2. Why is deadlock state is more critical then starvation? Describe resource
allocation graph with deadlock with a cycle but no deadlock.

Answer :

A fair system prevents starvation and deadlock. 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.

Here’s 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.

12 – ADARSH SINGH DIKHIT 2


Q3. What is deadlock? Explain necessary conditions for its occurrence.

Answer :

a deadlock is a state in which each member of a group is waiting for another member,
including itself, to take action, such as sending a message or more commonly releasing a
lock. Deadlock is a common problem in multiprocessing systems, parallel computing, and
distributed systems, where software and hardware locks are used to arbitrate shared
resources and implement process synchronization.

A deadlock situation on a resource can arise if and only if all of the following conditions
hold simultaneously in a system:

1. Mutual exclusion: At least one resource must be held in a non-shareable mode.


Otherwise, the processes would not be prevented from using the resource when
necessary. Only one process can use the resource at any given instant of time.
2. Hold and wait or resource holding: a process is currently holding at least one
resource and requesting additional resources which are being held by other
processes.
3. No preemption: a resource can be released only voluntarily by the process holding it.
4. Circular wait: each process must be waiting for a resource which is being held by
another process, which in turn is waiting for the first process to release the
resource. In general, there is a set of waiting processes, P = {P1, P2, …, PN}, such that
P1 is waiting for a resource held by P2, P2 is waiting for a resource held by P3 and so
on until PN is waiting for a resource held by P1.

12 – ADARSH SINGH DIKHIT 3

You might also like