Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OPERATING SYSTEMS
UNIT-III
2 | Page
Process states:
o Process P1 is holding an instance of resource type R2, and is
waiting for an instance of resource type R1.
o Process P2 is holding an instance of R1 and R2, and is waiting
for an instance of resource type R3.
o Process P3 is holding an instance of R3.
OPERATING SYSTEMS
3 | Page
4 | Page
OPERATING SYSTEMS
5 | Page
OPERATING SYSTEMS
6 | Page
There are two process solutions to critical section problem that satisfy
these three requirements. The processes are numbered P0 and P1.
Algorithm 1:One first approach is to let the process share a common integer
variable turn initialized to 0. If turn=0, then process P0 is allowed
to execute in its critical section. The structure of process P0 is
shown below:
OPERATING SYSTEMS
7 | Page
8 | Page
9 | Page
writing is performed
signal(wrt);
The structure of writer process
wait(mutex);
readcount:=readcount + 1;
if readcount =1 then wait(wrt);
signal(mutex);
reading is performed
wait(mutex);
readcount:= readcount - 1;
if readcount=0 then signal(wrt);
signal(mutex);
The structure of reader process
8. Explain briefly how we can recover from deadlocks.
When a deadlock is determined in a system by detection algorithm,
the operator be informed to deal with deadlock manually. The other
option is to let system recover from deadlock automatically. There are
two options for breaking the deadlock. One is simply to abort one or
OPERATING SYSTEMS
10 | P a g e
OPERATING SYSTEMS
11 | P a g e
12 | P a g e
repeat
wait (chopstick[i]);
wait (chopstick[(i+1)%5)
eat
signal (chopstick[i]);
signal (chopstick[(i+1)%5]);
think
until false;
Although this solution guarantees that no two neighbors are eating
simultaneously, it nevertheless must be rejected because it has the
possibility of creating a deadlock. Suppose that all five philosophers
become hungry simultaneously, and each grabs her left chopstick. All
the elements of chopstick will now be equal to 0. When each philosopher
tries to grab her right chopstick, she will be delayed forever.
Several possible remedies to the deadlock problem are listed below: Allow at most four philosophers to be sitting simultaneously at the
table.
OPERATING SYSTEMS
13 | P a g e
14 | P a g e
Safety Sequence : P0 can be relapsed after completed since its need is (0, 0, 0, 0)
OPERATING SYSTEMS
15 | P a g e
So,
So
So
OPERATING SYSTEMS
16 | P a g e
So
So
So
So
Safety sequence = < p0, p2, p3, p4 > Next P1 can complete
So, the safety sequence = < p0, p2, p3, p4, p1 >
So, the request made by P1 (0, 4, 2, 0) can be granted
immediately as the system is still in safe state
12. Explain wait-for-graph with example.
If all resources have only a single instance, then we can define a
deadlock J detection algorithm that uses a variant of the resource-allocation
graph, called J a wait-for graph. We obtain this graph from the resourceallocation graph by removing the nodes of type resource and collapsing the
appropriate edges.
An edge from P1 to Pj in a wait-for-graph implies that process Pi is
waiting for process Pj to release a resource that Pi needs. An edge Pi Pj
exists in a wait-for-graph if and only if the corresponding resource-allocation
graph contains two edges Pi Rq and Rq Pj for some resource Rq
A deadlock exists in a system if and only if the wait-for-graph contains a
cycle.
OPERATING SYSTEMS