Dining-Philosophers Problem
• Nphilosophers’ sit at a round table with a bowel of rice in the middle.
• They spend their lives alternating thinking and eating.
• They do not interact with their neighbors.
• Occasionally try to pick up 2 chopsticks (one at a time) to eat from
bowl
• Need both to eat, then release both when done
• In the case of 5 philosophers, the shared data
• Bowl of rice (data set)
• Semaphore chopstick [5] initialized to 1
3.
• a roundtable on which is set a large serving bowl of spaghetti, five plates, one for each
philosopher, and five forks. A philosopher wishing to eat goes to his or her assigned
place at the table and, using the two forks on either side of the plate, takes and eats
some spaghetti. The problem: Devise a ritual (algorithm) that will allow the
philosophers to eat. The algorithm must satisfy mutual exclusion (no two philosophers
can use the same fork at the same time) while avoiding deadlock and starvation
• Solution using Semaphore:-
• Each philosopher first picks up the fork on the left then the fork on the right. After the
philosopher is finished eating, the two forks are replaced on the table. This solution,
alas, leads to deadlock: If all of the philosophers are hungry at the same time, they all
sit down, they all pick up the fork on their left, and they all reach out for the other
fork, which is not there. In this undignified position, all philosophers starve.
4.
Dining-Philosophers Problem Algorithm
•Semaphore Solution
• The structure of Philosopher i :
while (true){
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
/* eat for awhile */
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
/* think for awhile */
}
• What is the problem with this algorithm?
5.
Deadlock
• Deadlock canbe defined as the permanent blocking of a set of
processes that either compete for system resources or communicate
with each other. A set of processes is deadlocked when each process
in the set is blocked awaiting an event (typically the freeing up of
some requested resource) that can only be triggered by another
blocked process in the set. Deadlock is permanent because none of
the events is ever triggered.
6.
Reusable resources
• Twogeneral categories of resources can be distinguished: reusable
and consumable. A reusable resource is one that can be safely used
by only one process at a time and is not depleted by that use.
Processes obtain resource units that they later release for reuse by
other processes. Examples of reusable resources include processors,
I/O channels, main and secondary memory, devices, and data
structures (such as files, databases, and semaphores).
7.
Consumable Resources
• Aconsumable resource is one that can be created (produced) and
destroyed (consumed). Typically, there is no limit on the number of
consumable resources of a particular type. An unblocked producing
process may create any number of such resources. When a resource is
acquired by a consuming process, the resource ceases to exist.
Examples of consumable resources are interrupts, signals, messages,
and information in I/O buffers.
8.
Deadlock Characterization/ Necessaryand sufficient
conditions for deadlock
• Mutual exclusion: only one thread at a time can use a resource
• Hold and wait: a thread holding at least one resource is waiting
to acquire additional resources held by other threads
• No preemption: a resource can be released only voluntarily by
the thread holding it, after that thread has completed its task
• Circular wait: there exists a set {T0, T1, …, Tn} of waiting threads
such that T0 is waiting for a resource that is held by T1, T1 is
waiting for a resource that is held by T2, …, Tn–1 is waiting for a
resource that is held by Tn, and Tn is waiting for a resource that
is held by T0.
Deadlock can arise if four conditions hold simultaneously.
10.
Resource-Allocation Graph
• Vis partitioned into two types:
• T = {T1, T2, …, Tn}, the set consisting of all the
threads in the system.
• R = {R1, R2, …, Rm}, the set consisting of all
resource types in the system
• request edge – directed edge Ti Rj
• assignment edge – directed edge Rj Ti
A set of vertices V and a set of edges E.
11.
Resource Allocation GraphExample
• One instance of R1
• Two instances of R2
• One instance of R3
• Three instance of R4
• T1 holds one instance of R2 and is
waiting for an instance of R1
• T2 holds one instance of R1, one
instance of R2, and is waiting for
an instance of R3
• T3 is holds one instance of R3
Basic Facts
• Ifgraph contains no cycles no deadlock
• If graph contains a cycle
• if only one instance per resource type, then deadlock
• if several instances per resource type, possibility of
deadlock
15.
Methods for HandlingDeadlocks
• Ensure that the system will never enter a deadlock
state:
• Deadlock prevention
• Deadlock avoidance
• Allow the system to enter a deadlock state and then
recover
• Ignore the problem and pretend that deadlocks
never occur in the system.
16.
Deadlock Prevention
• MutualExclusion – not required for sharable resources (e.g., read-only
files); must hold for non-sharable resources
• In general, the first of the four listed conditions cannot be disallowed. If access
to a resource requires mutual exclusion, then mutual exclusion must be
supported by the OS. Some resources, such as files, may allow multiple
accesses for reads but only exclusive access for writes. Even in this case,
deadlock can occur if more than one process requires write permission.
• Hold and Wait – must guarantee that whenever a thread requests a
resource, it does not hold any other resources
• Require threads to request and be allocated all its resources before it begins
execution or allow thread to request resources only when the thread has none
allocated to it.
• Low resource utilization; starvation possible
Invalidate one of the four necessary conditions for deadlock:
17.
• The hold-and-waitcondition can be prevented by requiring that a
process request all of its required resources at one time and blocking
the process until all requests can be granted simultaneously.
• This approach is inefficient in two ways. First, a process may be held
up for a long time waiting for all of its resource requests to be filled,
when in fact it could have proceeded with only some of the resources.
• Second, resources allocated to a process may remain unused for a
considerable period, during which time they are denied to other
processes. Another problem is that a process may not know in
advance all of the resources that it will require.
18.
Deadlock Prevention (Cont.)
•No Preemption:
• If a process that is holding some resources requests
another resource that cannot be immediately allocated
to it, then all resources currently being held are released
• Preempted resources are added to the list of resources
for which the thread is waiting
• Thread will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
• Circular Wait:
• Impose a total ordering of all resource types, and require
that each thread requests resources in an increasing
order of enumeration
19.
• In deadlockprevention, we constrain resource requests to prevent at least one
of the four conditions of deadlock.
• This is either done indirectly by preventing one of the three necessary policy
conditions (mutual exclusion, hold and wait, no preemption), or directly by
preventing circular wait.
• This leads to inefficient use of resources and inefficient execution of processes.
• Deadlock avoidance, on the other hand, allows the three necessary conditions
but makes judicious choices to assure that the deadlock point is never reached.
• As such, avoidance allows more concurrency than prevention. With deadlock
avoidance, a decision is made dynamically whether the current resource
allocation request will, if granted, potentially lead to a deadlock. Deadlock
avoidance thus requires knowledge of future process resource requests.
20.
• When anew thread enters the system, it must declare the maximum
number of instances of each resource type that it may need. This
number may not exceed the total number of resources in the system.
When a user requests a set of resources, the system must determine
whether the allocation of these resources will leave the system in a
safe state. If it will, the resources are allocated; otherwise, the thread
must wait until some other thread releases enough resources.
21.
Banker’s Algorithm
• Multipleinstances of resources
• Each thread must a priori claim maximum use
• When a thread requests a resource, it may have to
wait
• When a thread gets all its resources it must return
them in a finite amount of time
22.
Data Structures forthe Banker’s Algorithm
• Available: Vector of length m. If available [j] = k, there
are k instances of resource type Rj available
• Max: n x m matrix. If Max [i,j] = k, then process Ti may
request at most k instances of resource type Rj
• Allocation: n x m matrix. If Allocation[i,j] = k then Ti is
currently allocated k instances of Rj
• Need: n x m matrix. If Need[i,j] = k, then Ti may need k
more instances of Rj to complete its task
Need [i,j] = Max[i,j] – Allocation [i,j]
Let n = number of processes, and m = number of resources types.
23.
Safety Algorithm
1. LetWork and Finish be vectors of length m and n,
respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe
state
24.
Resource-Request Algorithm forProcess Pi
Requesti = request vector for process Ti. If Requesti [j] =
k then process Ti wants k instances of resource type Rj
1. If Requesti Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2. If Requesti Available, go to step 3. Otherwise Ti must wait,
since resources are not available
3. Pretend to allocate requested resources to Ti by modifying the
state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• If safe the resources are allocated to Ti
• If unsafe Ti must wait, and the old resource-allocation state is
restored
25.
Banker’s Algorithm
Process MaxAllocated Remaining need=
(max-allocated)
Available
A B C A B C A B C A B C
3 3 2
P0 7 5 3 0 1 0
P1 3 2 2 2 0 0
P2 9 0 2 3 0 2
P3 2 2 2 2 1 1
P4 4 3 3 0 0 2
A=10, B=5 C=7. 1. Calculate NEED matrix 2. Find the safe state sequence
If for any process, resources needed < resources available, then that process can be
allocated with the resources
26.
Banker’s Algorithm
Process MaxAllocated Remaining need=
(max-allocated)
Available
A B C A B C A B C A B C
3 3 2
P0 7 5 3 0 1 0 7 4 3
P1 3 2 2 2 0 0 1 2 2 5 3 2
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1
P4 4 3 3 0 0 2 4 3 1
A=10, B=5 C=7
Let process sequence is P1 first
Then available resources after P1 uses n releases its resources= available+allocated=5 3 2
From 5 3 2, need of P3 can be satisfied
27.
Banker’s Algorithm
Process MaxAllocated Remaining need=
(max-allocated)
Available
A B C A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3
P1 3 2 2 2 0 0 1 2 2 5 3 2
P2 9 0 2 3 0 2 6 0 0
P3 2 2 2 2 1 1 0 1 1 7 4 3
P4 4 3 3 0 0 2 4 3 1
A=10, B=5 C=7
Let process sequence is P1 first
Then available resources after P1 uses n releases its resources= available+allocated=5 3 2
From 5 3 2, need of P3 can be satisfied.
Hence sequence becomes P1-> P3
28.
Banker’s Algorithm
Process MaxAllocated Remaining need=
(max-allocated)
Available
A B C A B C A B C A B C
P0 7 5 3 0 1 0 7 4 3 7 5 3
P1 3 2 2 2 0 0 1 2 2 5 3 2
P2 9 0 2 3 0 2 6 0 0 10 5 5
P3 2 2 2 2 1 1 0 1 1 7 4 3
P4 4 3 3 0 0 2 4 3 1 10 5 7
A=10, B=5 C=7
Then available resources after P1 uses n releases its resources= available+allocated=7 4 3
From 7 4 3, need of P0 can be satisfied.
Hence sequence becomes P1-> P3->P0->P2->P4
29.
Banker’s Algorithm
Process MaxAllocated Remaining need=
(max-allocated)
Available
A B C D A B C D A B C D A B C D
3 3 2
P0 7 5 3 0 1 0
P1 3 2 2 2 0 0
P2 9 0 2 3 0 2
P3 2 2 2 2 1 1
P4 4 3 3 0 0 2
A=10, B=5 C=7
If for any process, resources needed < resources available, then that process can be
allocated with the resources
30.
Deadlock Detection
• Allowsystem to enter deadlock state
• Detection algorithm
• Recovery scheme
• a detection-and-recovery scheme requires
overhead that includes not only the run-time costs
of maintaining the necessary information and
executing the detection algorithm but also the
potential losses inherent in recovering from
adeadlock.
31.
Single Instance ofEach 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.
• Maintain wait-for graph
• Nodes are threads
• Ti Tj if Ti is waiting for Tj
• An edge from Ti to Tj in a wait-for graph implies that thread Ti is waiting
for thread Tj to release a resource that Ti needs.
• Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock
• An algorithm to detect a cycle in a graph requires an order of n2
operations, where n is the number of vertices in the graph
32.
Resource-Allocation Graph andWait-for Graph
Resource-Allocation Graph Corresponding wait-for graph
An edge Ti → Tj exists in a wait-for graph if and
only if the corresponding resource-allocation
graph contains two edges Ti → Rq and Rq → Tj
for some resource Rq.
33.
Several Instances ofa Resource Type
• The wait-for graph scheme is not applicable to a resource-
allocation system with multiple instances of each resource type.
• Detection algorithm uses several data structures as used in
Banker’s Algorithm.
• Available: A vector of length m indicates the number of available
resources of each type
• Allocation: An n x m matrix defines the number of resources of
each type currently allocated to each thread.
• Request: An n x m matrix indicates the current request of each
thread. If Request [i][j] = k, then thread Ti is requesting k more
instances of resource type Rj.
34.
Detection Algorithm
1. LetWork and Finish be vectors of length m and n, respectively Initialize:
a) Work = Available [work = no of resources available]
b) For i = 1,2, …, n, if Allocationi 0, then [ if resources are allocated]
Finish[i] = false; [process not finished]
otherwise, Finish[i] = true
2. Find an index i such that both: [ith
process]
a) Finish[i] == false [process is running]
b) Requesti Work [requested < available]
If no such i exists, go to step 4
3. Work = Work + Allocationi [allocate resources]
Finish[i] = true
go to step 2
4. If Finish[i] == false, [i.e. process is waiting] for some i, 1 i n, then the
system is in deadlock state. Moreover, if Finish[i] == false, then Ti is
deadlocked