OS Mod 3 by ?

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Chapter 3: Deadlocks & Memory Management

Deadlock
3.1 System model:

Whenever a process has to use a resource, it has to request it & release it


after using it. A process can request more than one resource as per its
requirement. The resources requested by any process should not exceed
the available resources.

The sequence for utilizing resources:

1. Request: If a process needs a particular resource it has to request


it. It may be granted immediately or it has to wait.
2. Use: A process should operate on the resource.
3. Release: After using the resource, it has to release it.

A resource is free or allocated will be recorded in a system table.


Whenever a process requests a resource, if the resource is free it will be
allocated otherwise the process has to wait in the queue of processes
which needs that resource.

Consider a scenario; system consists of one printer & one CD driver.


The process Pl is holding CD drive & process P2 is holding printer.
Then it P 1 requests Printer & P2 requests CD drive, and then a deadlock
occurs.

3.2 Deadlock characterization

Necessary conditions for deadlock

l . Mutual exclusion: only once process can use a resource at any


point of time, if any other process request the same resource it
will be delayed until the resource is released.
2. Hold & wait: A process holding a resource & waiting for another
resource which is already held by another process.

92
3. No preemption: Resources should not be preempted when it is
held by a process. It should be released after completion of the
task or the resource should be released voluntarily by the
process.
4. Circular wait: Consider a set of processes {Po, P1 . . .. ... .. .. . ..... .
Pn}- The process Po is waiting for resource held by P1 , P1 is
waiting for resource held by P2. Pn-1 is waiting for resource held
by Pn. Pn is waiting to resource held by Po

Resource Allocation Graph


A resource allocation graph consists of set of vertices V & set of edges
E. The set of vertices V is partitioned into 2 types of nodes P= {P 1, P2
...... P n}, the set of processes &
R = {R1, R2 ... ... Rn} the set of resource types,
Pi • Ri indicates that the process P i Requested the resource the Ri.
This directed edge is called request edge.
Ri • Pi indicates that the resource type Ri has been allocated to
process Pi.
This directed edge is called assignment edge.

n2. • • •

Rl1 •••
Fl3 • •

Figure 3.1 Resource-allocation Graph

The fig 3.1 indicates the following situation of the sets - P, R & E

93
• P = { Pi~ P2]
• R = (Rt. R,, R:l. R,]

• E

Resource instances

• One instance of resource type R1


• 3 instance of resource type R2
• 2 instance of resource type R3
• 3 instance of resource type ~

Process states

P, is holding resource type R3 & waiting for an instance of resource type


R,

P2is holding the resource type ~ & waiting for resource type R2.

In the resource allocation graph, if there are no cycle than there is no


deadlock

If there is a cycle, there may be a deadlock.

The following two examples show cycle in resource allocation graph


which results in a deadlock & a cycle in a resource allocation graph
which does not result in a deadlock.

94
Figure 3.2 Resource allocation graph with a deadlock.

Figure 3.3 Resource allocation graph with a cycle but no deadlock.

The P4 may release an instance of R2 & the resource can be allocated to


P3, breaking the cycle

95
3.3 Methods for handling deadlock

Deadlocks are handled in one of the following ways.

1. Using an algoritlnn to prevent deadlock


2. Allow the system to enter deadlock state & detect & recover it.
3. Ignore the problem

3.4 Deadlock Prevention

Mutual Exclusion:

Non sharable resources require mutual exclusion

For e.g. Printer cannot be shared by more than one process.

Sharable resources do not require mutual exclusion

For e.g. A read only file can be shared by more than are process.

Hold & wait:

Two approaches for hold & wait.

I. A process can allocate all the resources. Initially to execute its


task & when its task finishes release all resources.
E.g. : A process may copy the contents from DVD drive to a file on disk
& then print it.

When the printing task completes, release all the resources.

2. A process may be allocated only few resources it needs initially,


additional resources may be allocated after releasing the
allocated resources.
E.g. : A process may be initially allocated with DVD drive & disk, once
the copy is over then release DVD drive & disk. The printer & the file
may be allocated till it completes the task.

96
Both the approaches have two disadvantages

1. Resource utilization is low.


2. It may lead to starvation.

No Preemption:

When a process requests a resource, if the resource is used by some other


process, it cannot be granted immediately. The process has to wait & the
resources are added to the list of resources which the process is waiting.
When all resources are available to the process then the process is
restarted & given with all resources.

Circular wait:

We can ensure that circular wait never occurs. The resources need to be
ordered & each process should request the resource in an increasing
order of enumeration.

Suppose a process request a resource Ri & then if it request a resource of


type R;, it can be allocated only if R/ s order of enumeration is higher
thanRi.

3.5 Deadlock Avoidance

Safe State

If there is a safe sequence then there exists a safe state. A process has to
declare maximum no of resources which it needs. With that information
we need to design an algorithm which prevents the deadlock.

Consider a sequence {P 1, P2 ...... P n} .The process P, may request for


resource held by P2 Then the resource will not be allotted immediately.
P, has to wait until P2 releases the resource requested by the P, & so on.

Once the process Pi completes its task & terminates. Then the process
Pi+, may be allocated with the required resources & so on.

97
Then we say that the system is in safe state. Otherwise it is in unsafe
state.

A deadlock state is an unsafe state. But not all unsafe states are deadlock
states.

Resource allocation - graph algorithm

Pi~Rj is referred to as claim edge, which indicates the process Pi may


request resource Rj at some time in the future.

If we convert edge Pi~Rj to an assignment edge Rj~ Pi & if it does not


possess a cycle, then the request can be grated.

If there exist no cycles these allocation of resources will leave the system
in safe state. If there exists a cycle then the allocation of resources will
leave the system in unsafe state.

Bankers Algorithm

If we have multiple instances of resources than resource allocation graph


algorithm is not suitable. So we apply Bankers algorithm in such cases.
When a new process enters the system the following things should be
ensured.

1. The process has to declare its need.


2. The need should not exceed the maximum resources m the
system.
3. If any request for resource is made then the algorithm checks
whether the allocation of resources leads to the safe state of the
system or not. If system remains in the safe state than the
resources can be allocated, otherwise the process has to wait.
The following data structures are required to implement Bankers
Algorithm.

1. Available: Number of resources available.


2. Max: Maximum demand of each process.

98
3. Allocation: Number of resources of each type currently allocated
to each process.
4. Need: Need of remaining resources by each process. Need is
computed as follows
Need = Max - Allocation.
Safety algorithm

1. Initialize Currently_available - Available. Finish[i] = FALSE.


(i=O, 1..... .... n-1)
2. Find i such that
a. Finish[i] =FALSE
b. Need <=Currently_available
If no such i exists go to step 4

3. Currently_available = Currently_available+ Allocation.


Finish[i] = TRUE
Go to step 2.
4. If finish[i] = TRUE for all i than the system is in safe state,
otherwise it is in unsafe state.
Resource-req nest algorithm

I. Request (for process i) <= Need


2. Request (for process i) <= Available
3. Pretend like allocating resources to the requested process by
doing following modifications.
Available = Available - Request
Allocation = Allocation + Request
Need = Need - Request
Then apply the safety algorithm if it leaves the system in a safe state
than we can allocate the requested resource otherwise the process has to
wait until the resources are released by other processes. Modification
done is not retained if the resources are not allocated to the requested
process.

99
Bankers Algorithm -Problems

Problem No I-Bankers Algorithm

Given the following information

Allocation Maximum Available

Jobs A B C A B C A B C

Pl 1 0 2 4 1 2 2 2 0

P2 0 3 1 1 5 1

P3 1 0 2 1 2 3

Is the system in safe state?

We have to compute Need Matrix.

Need = Maximum - Allocation

Need

A B C

Pl 3 1 0

P2 1 2 0

P3 0 2 1

Now the available matrix is <2, 2, O>

We have to check whether we can satisfy any of the process's need. We


can't satisfy PI because PI requires 3 instances of resource A. But
available is only 2 instances of resource A. We can't satisfy P3 also
because P3 requires I instance of resource C. But not even I instance of
resource C is available.
100
P2 requires 1 instance of resource A, 2 instances of resource B & 0
instance of resource C< l,2,0>. These are less than or equal to available
matrix<2,2,0>. So let's allocate resources for P2. P2 starts using
resources & when P2 terminates, it releases all its allocated resources.
Now we have to compute currently available resources as follows.

We have to add the available matrix with the resources released by P2.
P2 was allocated <0,3, l >(Look at allocation matrix).

Available Matrix <2,2,0>

Resources released by P2 < 0,3,l >

Currently available <2,5,l>

Now we have to check whether we can satisfy any of the process's need.
We have to consider remaining processes now, they are Pl & P3 . Need
of Pl is <3,1,0> cannot be allocated because we have only 2 instances of
resource A(look at currently available matrix<2,5,l>), but Pl needs 3
instances of resource A(look at need matrix). P3 need can be satisfies.

<0,2, l ><= <2,5, l > (P3 need <= currently available)

So allocate resources to P3. When P3 terminates it releases all its


allocated resources. Now the currently available resource can be
computed as follows.

Add currently available matrix (before P3 released its resources) with


resources released by P3 .

Currently available (before P3 released its resources) ... ....... .. . <2,5,1>

Resources released by P3 .. .......... ... ............ .. .......... ....< 1,0,2>

New available resources ..................... ............ . .. ......... . <3,5,3>

Now the currently available resource matrix is <3,5,3>

101
we can satisfy Pl's need, since Pl need<3,l,0><= Currently available
<3,5,3>.

Allocate resources for Pl.

Now all the processes were allocated resources & they executed. The
system is in safe state. Safe sequence is P2,P3,Pl.

Problem No 2-Bankers Algorithm

Given the foilowing information

Allocation Maximum Available

Jobs A B C D A B C D A B C D
PO 0 0 1 2 0 0 1 2 1 5 2 0
Pl 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Is the system in safe state?

If Pl request for <O,4,2,O>, can the request be granted immediately?

We need to compute need matrix

Need = Maximum - Allocation

Need
A B C D
PO 0 0 0 0
Pl 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2

102
Now let us consider PO

Need of PO is <O O O O>

Available Matrix is < 1,5,2,0>

Need<=Available

<O O O O><= <1,5,2,0> is true

So we can allocate resources to PO

Available Matrix < 1,5,2,0>


(Before allocating resources to PO)
Resources Released by PO <O O 1 2>
New Available Matrix < l 5 3 2>
(after adding available matrix with resources released
by PO)

Now let us consider Pl

Need of Pl is <O 7 5 O>

New Available Matrix is <1 5 3 2>

Need<=A vailable

<O 7 5 O><= <1 5 3 2> is false

So we cannot allocate resources to P 1 at this point of time.

Now let us consider P2

Need of P2 is < 1 0 0 2>

Available Matrix is < 1 5 3 2>

103
Need<=Available

< 1 0 0 2><= <1 5 3 2> is true

So we can allocate resources to P2

Available Matrix < l 5 3 2>


(Before allocating resources to P2)
Resources Released by P2 < 1354>
New Available Matrix <2 8 8 6>
(after adding available matrix with resources
released by P2)

Now let us consider Pl

Need of Pl is <O 7 5 O>

Available Matrix is <2 8 8 6>

Need< Available

<O7 5 0><= <2 8 8 6> is true

So we can allocate resources to PI

Available Matrix <2 8 8 6>


(Before allocating resources to P 1)
Resources Released by P 1 <100 O>
New Available Matrix <3 8 8 6>
(after adding available matrix with resources
released by P 1)
Now let us consider P3

Need of P3 is <O O 2 O>

Available Matrix is <3 8 8 6>

Need<=A vailable

104
<O O 2 0 ><= <3 8 8 6> is true .

So we can allocate resources to P3

Available Matrix <3 8 8 6>


(Before allocating resources to P3)
Resources Released by P3 <O 6 3 2>
New Available Matrix <3 14 11 8>
(after adding available matrix with resources
released by P3)

Now let us consider P4

Need of P4 is <O 6 4 2>

Available Matrix is <3 14 11 8>

Need<=Available

<0642><=<3 1411 8> istrue .

So we can allocate resources to P4

Available Matrix <3 14 11 8>


(Before allocating resources to P4)
Resources Released by P4 <O O 14>
New Available Matrix <3 14 12 12 >
(after adding available matrix with resources
released by P4)
The system is in safe state

Safe Sequence is P0,P2,Pl,P3,P4

Now the additional request of <0,4,2,0> by Pl is made.

105
We have to do following modification & then apply safety algorithm

I. Is Request for PI <= Need for PI?


<0,4,2,0><= <0,7,5,0> this is TRUE.
2. Is request for Pl <= Available
<0,4,2,0><= <1,5,2,0> this is TRUE.
3. Following modification is made
Available= Available -Request
< 1,5,2,0> - <0,4,2,0> = <1,1,0,0>
Allocation = Allocation + Request
< 1,0,0,0> + <0,4,2,0> = < 1,4,2,0>
Need = Need- Request
<0,7,5,0>- <0,4,2,0> = <0,3,3,0>

Now need for Pl is <0,3,3,0>


Available matrix is <1,1,0,0>
Allocation of Pl is <1,4,2,0>

New Snapshot

Allocation Maximum Available

Jobs A B C D A B C D A B C D
PO 0 0 1 2 0 0 1 2 1 1 0 0
Pl 1 4 2 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

New Need Matrix


A B C D
PO 0 0 0 0
Pl 0 3 3 0

106
P2 1 0 0 2

P3 0 0 2 0
P4 0 6 4 2

Now apply safety algorithm.


Now let us consider PO

Need of PO is <OO OO>

Available Matrix is <I, 1,0,0>

Need<=Available

<OOOO><= <l ,1,0,0> is true

So we can allocate resources to PO

Available Matrix < l, 1,0,0>


(Before allocating resources to PO)
Resources Released by PO <OO l 2>
New Available Matrix <I, 1,1,2>
(after adding available matrix with resources
released by PO)
Now let us consider Pl
Need of Pl is <O3 3 O>
Available Matrix is <I 1 l 2>
Need<=Available
<O3 3 O><= <1 1 1 2> is false.

So we cannot allocate resources to Pl at this point of time.

Now let us consider P2

Need of P2 is <1 0 0 2>

107
Available Matrix is < I I I 2>

Need<=A vailable

< I O O 2><= <I I I 2> is true

So we can allocate resources to P2

Available Matrix < 1 1 1 2>


(Before allocating resources to P2)
Resources Released by P2 <1354>
New Available Matrix <2 4 66>
(after adding available matrix with resources released
byP2)

Now let us consider P3


Need of P3 is <O O 2 O>
Available Matrix is <2 4 6 6>
Need<=Available
<O O 2 0 ><= <2 4 6 6> is true
So we can allocate resources to P3

Available Matrix <2 4 6 6>


(Before allocating resources to P3)
Resources Released by P3 <O 6 3 2>
New Available Matrix <2 IO 9 8>
(after adding available matrix with resources released
byP3)

Now let us consider Pl

Need of Pl is <O 3 3 O>

Available Matrix is <2 10 9 8>


108
Need<=A vailable

<O 3 3 O><= <2 10 9 8> is true

So we can allocate resources to P 1

Available Matrix <2 10 9 8>


(Before allocating resources to P 1)
Resources Released by P 1 < l 4 2 O>
New Available Matrix <3 14 11 8>
(after adding available matrix with resources released by
Pl)
Now let us consider P4

Need of P4 is <O 6 4 2>

Available Matrix is <3 14 11 8>

Need<=A vailable

<O 6 4 2><= <3 14 11 8> is true .

So we can allocate resources to P4

Available Matrix <3 14 11 8>


(Before allocating resources to P4)
Resources Released by P4 <O O 1 4>
New Available Matrix <3 14 12 12
(after adding available matrix with resources released by >
P4)

All processes have been allocated the resources & executed. Now the
system is in safe state even after the additional request made by Pl is
granted. So the request can be immediately granted. Safe sequence
after the additional request by Pl is granted-> PO,P2,P3,Pl,P4.

109
Problem No 3-Bankers Algorithm

System Consists of 5 jobs (Pl,P2,P3,P4,P5) & 3 Resources


(Rl,R2,R3). Resource type Rl has 10 instances; resource type R2 has
S instances; and R3 has 7 instances. The following snapshot of the
system has been taken.

Allocation Maximum Available


Jobs R1 R2 R3 Rl R2 R3 Rl R2 R3
Pl 0 1 0 7 s 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
PS 0 0 2
Find the need matrix & calculate the safe sequence by using
banker's algorithm. Mention the above system is safe or not.

If an additional request from P2 arrives for <l O 2> can the request
be granted immediately?

Answer:

Need = Maximum - Allocation

Need Matrix Max-Allocation


Rl R2 R3

Pl 7 4 3
P2 1 2 2
P3 6 0 0
P4 0 1 1
PS 4 3 1

110
Now let us consider Pl

Need of Pl is <7 4 3>

Available Matrix is <3 3 2>

Need<=A vailable

<7 4 3><= <3 3 2> is false

So we cannot allocate resources to Pl at this point of time

Now let us move to next job/process P2

Need of P2 is < 1 2 2>

Available Matrix is <3 3 2>

Need <= Available

< l 2 2><= <3 3 2> is true

So we can allocate resources to P2

Available Matrix <3 3 2>


(Before allocating resources to P2)
Resources Released by P2 <200>
New Available Matrix <5 32>
(after adding available matrix with resources released by
P2)
Now consider P3

Need of P3 is <6 0 O>

New Available is <5 3 2>

Need <= Available

<6 0 O><= <5 3 2> is false

111
So we cannot allocate resources to P3 at this point of time

Now let us consider P4

P4 need is <0 1 1>

New available matrix is <5 3 2>

Need <= Available

<0 1 1 >< = <5 3 2> is true

So we can allocate resources to P4

Available Matrix <5 32>


(Before allocating resources to P4)
Resources Released by P4 <2 1 l>
New Available Matrix <7 43>
(after adding available matrix with resources released by
P4)

Now let us consider P5

P5 need is <4 3 l >

New Available Matrix is <7 4 3>

Need <= Available

<4 3 l ><= <7 4 3> is true

So we can allocate resources to P5

Available Matrix <7 43>


(Before allocating resources to PS)
Resources Released by PS <002>
New Available Matrix <7 45>
(after adding available matrix with resources released by P5)

112
Now P 1 & P3 are pending

Pl need is <7 4 3>

New Available Matrix is <7 4 5 >


Need <= Available
<7 4 3><= <7 4 5> is true
Even though need <= available this request cannot be granted at this
point of time, because it may lead to deadlock or we don't get a safe
sequence.
Now consider P3
Need of P3 is <6 0 0 >
New Available is <7 4 5 >
Need <= Available
<6 0 O><= <7 4 5> is true
So we can allocate resources to P3
Available Matrix <7 45>
(Before allocating resources to P3)
Resources Released by P3 <3 02>
New Available Matrix <IO 4 7>
(after adding available matrix with resources released by
P3)

Now let us consider Pl

Pl need is <7 4 3>

New Available Matrix is <l O 4 7 >

Need <= Available

<7 4 3><= <10 4 7> is true

So we can allocate resources to PI

113
Available Matrix < 10 4 7>
(Before allocating resources to P 1)
Resources Released by P 1 <0 1 0>
New Available Matrix < 10 5 7>
(after adding available matrix with resources released by
Pl)

Now the system is in safe state

Safe sequence is P2,P4,P5,P3,Pl

If an additional request from P2 arrives for <1 0 2> can the request
be granted immediately?

Answer

Now the additional request < 1 0 2 > by P2 has been made

We have to do following modifications & then apply safety algorithm

1. Is request for P2 <= Need for P2?


< 1 0 2 ><= < 1 2 2> is true
2. Is request for P2 <= Available?
< l O 2><= <3 3 2> is true
Now following modification is made

Available = Available - Request

= <3 3 2> - < 1 0 2>


=<2 3 0>

Allocation = Allocation + Request

= <2 0 0> + < 1 0 2>

=<3 0 2>

Need = Need - Request

114
=< l 2 2> - < l O2>

=<O 2 O>

Now Need for P2 is <O2 O>

Available Matrix is <2 3 O>

Allocation for P2 is <3 0 2>

Now let us rewrite the snapshot & need matrix

Allocation Maximum Available


Jobs R1 R2 R3 Rl R2 R3 Rl R2 R3
Pl 0 1 0 7 s 3 2 3 0
P2 3 0 2 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
PS 0 0 2

Need Matrix=Max-Allocation
Rl R2 R3
Pl 7 4 3
P2 0 2 0
P3 6 0 0
P4 0 1 1
PS 4 3 1

Please note the 3 changes.

Now let us apply safety algorithm

115
Let us consider PI

Pl need is <7 4 3> is more than available <2 3 O>. So we cannot allocate
resources to PI at this point of time.

Let us Consider P2

P2 need <O 2 O><= <2 3 O> Available

Is true

So allocate resources to P2

Available Matrix <2 3 O>


(Before allocating resources to P2)
Resources Released by P2 <3 02>
New Available Matrix <5 3 2>
(after adding available matrix with resources released by
P2)

Let us consider P3

Need <= Available

<6 0 O><= <5 3 2> is false

So we cannot allocate resources to P3 at this point of time.

Let us consider P4

Need <=Available

<O I I><= <5 3 2> is true

So allocate resources to P4

116
Available Matrix <5 3 2>
(Before allocating resources to P4)
Resources Released by P4 <2 11>
New Available Matrix <7 43>
(after adding available matrix with resources released by
P4)

Now let us consider PS

Need <= Available

<4 3 1><= <7 4 3> is true

Available Matrix <7 43>


(Before allocating resources to PS)
Resources Released by PS <0 02>
New Available Matrix <7 45>
(after adding available matrix with resources released by
PS)

Now P 1 & P3 are pending

Let us consider Pl

Need <= Available

<7 4 3><= <7 4 5> is true

This request cannot be granted at this point of time because we may not
get safe sequence.

Let us consider P3

Need <= Available

<6 0 0><= <7 4 5 > is true

117
Available Matrix <7 45>
(Before allocating resources to P3)
Resources Released by P3 <3 02>
New Available Matrix < 10 4 7>
(after adding available matrix with resources released by
P3)

Now let us consider P 1


Need <= Available
<7 4 3><= < 10 4 7> is true

Available Matrix < 10 4 07>


(Before allocating resources to P 1)
Resources Released by P 1 <O 1 O>
New Available Matrix < 10 5 7>
(after adding available matrix with resources released by
Pl)

All processes have been allocated the resources & executed. Now the
system is in safe state even after the additional request made by P2 is
granted. So the request can be immediately granted. Safe sequence
after the additional request by P2 is granted -> P2,P4,P5,P3,Pl

Problem No 4-Bankers Algorithm

Consider the following snapshot of the system

Allocation Maximum Available


Jobs A B C A B C A B C
PO 0 0 2 0 0 4 1 0 2
Pl 1 0 0 2 0 1
P2 1 3 5 1 3 7
P3 6 3 2 8 4 2
P4 1 4 3 1 5 7

118
Answer the following questions using Bankers Algorithm.

1. Is the system in a safe state?


2. Ha request from P2 arrives for <O O2> can the request be
granted immediately?
Answer:

Need Matrix = Maximum - Allocation

Need Matrix Max-Allocation


A B C
PO 0 0 2
Pl 1 0 1
P2 0 0 2
P3 2 1 0
P4 0 1 4

Let us consider PO

Need <= Available

<O O 2><= < 1 0 2> is true

So we can allocate resources to PO

Available Matrix < 1 02>


(Before allocating resources to PO)
Resources Released by PO <O 02>
New Available Matrix < I 04>
(after adding available matrix with resources released by
PO)

119
Let us consider Pl

Need <= Available

< l O l ><= < 1 0 4> is true

So we can allocate resources to P 1

Available Matrix < l 04>


(Before allocating resources to P 1)
Resources Released by P 1 < l 00 >
New Available Matrix <204>
(after adding available matrix with resources released by
Pl)

Let us consider P2

Need <= Available

<O O 2><= < 2 0 4> is true

So we can allocate resources to P2

Available Matrix <2 04 >


(Before allocating resources to P2)
Resources Released by P2 <l 3 5 >
New Available Matrix <3 3 9>
(after adding available matrix with resources released by
P2)

Let us consider P3

Need <= Available

<2 1 0><= < 3 3 9> is true

So we can allocate resources to P3

120
Available Matrix <3 3 9 >
(Before allocating resources to P3)
Resources Released by P3 <6 3 2 >
New Available Matrix <9 6 11 >
(after adding available matrix with resources released by
P3)

Let us consider P4

Need <= Available

<O 1 4><= < 9 6 11 > is true

So we can allocate resources to P4

Available Matrix <9 6 11 >


(Before allocating resources to P4)
Resources Released by P4 < 14 3 >
New Available Matrix < 10 10 14>
(after adding available matrix with resources released by
P4)

All the processes are allocated with the required resources and they
executed. So the system is in safe state. Safe Sequence is PO, PI , P2, P3 ,
P4

If a request from P2 arrives for <O O 2> can the request be granted
immediately?

Answer:

Now the additional request <O O 2> by P2 is made

We have to do the following modification & then apply safety algoritlun

I. ls request for P2 <= Need for P2?


<O O 2><= <O O 2> is true.

121
2. Is request for P2 <= Available?
<OO2><= <l O2> is true.
Following modifications are made

Available = Available-Request

= <102> - < 0 0 2> = <100 >

Allocation = Allocation + Request

< 1 3 5> + <OO 2> = < 1 3 7>

Need = Need - Request

= <O O2> - <OO2> = <O OO>

Now the need for P2 is <O O O>

Available Matrix is <1 0 0 >

Allocation for P2 is < 1 3 7>

Now let us rewrite the snapshot & Need Matrix

Allocation Maximum Available


Jobs A B C A B C A B C
PO 0 0 2 0 0 4 1 0 0
Pl 1 0 0 2 0 1
P2 1 3 7 1 3 7
P3 6 3 2 8 4 2
P4 1 4 3 1 5 7

Need Matrix-Max-Allocation
A B C
PO 0 0 2

122
Pl 1 0 1
P2 0 0 0

P3 2 1 0
P4 0 1 4

Please note 3 Changes in Tables.

Now apply the safety algorithm

Let us Consider PO

Need <= Available

<O O 2><= <1 0 0 > is false


We cannot allocate resources to PO at this pint of time.

Let us consider PI
Need <= Available

<1 0 I><= <1 0 O> false


We cannot allocate resources to P 1 at this pint of time.

Let us consider P2

Need <= Available


<O O O><= < 1 0 0 > is true
So allocate resources to P2

Available Matrix <1 00 >


(Before allocating resources to P2)
Resources Released by P2 <137 >
New Available Matrix <2 3 7>
(after adding available matrix with resources released by
P2)

123
Let us consider P3

Need <= Available

<2 1 0 ><= <2 3 7 > is true

So allocate resources to P3

Available Matrix <2 3 7>


(Before allocating resources to P3)
Resources Released by P3 <6 3 2 >
New Available Matrix <8 69>
(after adding available matrix with resources released by
P3)

Let us consider P4

Need <= Available

<O I 4 ><= <8 6 9 > is true

So allocate resources to P4

Available Matrix <8 69 >


(Before allocating resources to P4)
Resources Released by P4 <143 >
New Available Matrix <9 10 12>
(after adding available matrix with resources released by
P4)
Let us consider PO

Need <= Available

<O O 2 ><= <9 10 12 > is true

So allocate resources to PO

124
Available Matrix <9 10 12 >
(Before allocating resources to PO)
Resources Released by PO <O 02 >
New Available Matrix <9 10 14>
(after adding available matrix with resources released by
PO)

Let us consider PI

Need <= Available

< 1 0 1 ><= <9 10 14 > is true

So allocate resources to P 1

Available Matrix <9 10 14 >


(Before allocating resources to Pl)
Resources Released by P 1 < 1 00 >
New Available Matrix < 10 10 14>
(after adding available matrix with resources released by
Pl)

All processes have been allocated the resources & executed. Now the
system is in safe state even after the additional request made by P2 is
granted. So the request can be immediately granted. Safe sequence
after the additional request by P2 is granted -> P2, P3, P4, PO, Pl
Problem No 5-Bankers Algorithm

The Operating System contain 3 resources, the number of instances


of each resource type are 7,7,10. The current resource allocation
state is as shown below.

Processes Current Allocation Maximum Need


Rl R2 R3 Rl R2 R3
Pl 2 2 3 3 6 8
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4
125
Is the current allocation in a safe state?
Answer

Available=Number of instances - Sum of allocation.

Processes Current Allocation


Rl R2 R3
Pl 2 2 3
P2 2 0 3
P3 1 2 4
5 4 10

Available= <7 7 10> - <5 4 10> <2 3 0>

Need = Max-Allocation
R1 R2 R3
Pl 1 4 5
P2 2 3 0
P3 2 2 0

The System is in safe state.

Safe sequence is P3, P2, Pl.

3.6 Deadlock detection:

Single instance of each resource type

In case if all resources are having single instance, then we can use a
variant of resource allocation graph (wait for graph) to detect deadlock.

Pi--+Pj indicates that the process Pi is waiting for Pj to release the


resource which Pi needs.

Pi--+ Rx & Rx--+Pj indicates that Pi needs Rx & Rx is allotted to Pj.

126
This wait for graph should not contain cycle to consider the systems as
safe state

It there exists a cycle in wait for graph then, the system in unsafe state

Al -
~: -

Resource-allocation graph Corresponding Wait-for graph

3.7 Recovery from deadlock

Process termination

We use following 2 methods to abort process & there by eliminate


deadlock.
• Abort all deadlocked processes
• Abort one process at a time until the deadlock cycle is eliminated

Resource preemption:

The following issues needs to be addressed if preemption has to be


employed.

1. Selecting a victim: which process & which resource needs to be


preempted?
2. Rollback: If a resource is preempted what do we do with the
process?
3. Starvation: How to ensure that the starvation will not occur?
127
Memory Management
3.8 Background

Linking:

The task performed during linking is resolving external references. At


the time source program is translated into object module, external
references cannot normally be resolved. The translator generates an
external symbol table for each module which lists all external references
used in the module. Object modules generated by assemblers may
contain unresolved references. These are resolved by using other object
module by using linker.

Loading:

Copying the object module into memory

Logical address space:

The set of all logical addresses

Physical address space:

The set of all physical addresses.

Static linking:

Occurs at compile time, hence it occurs prior to loading a program.


With static linking the external symbols are resolved at compile
time.

Dynamic linking:

Dynamic Linking occurs at run time, so it occurs after or at the


time of loading a program. With dynamic linking external symbols
are resolved either at loading time or at run time.

128
Symbol resolution:

A program is made up of multiple subprograms. Reference to one


subprogram to another is made through symbols.

Note: Compilers and assemblers generate re-locatable object files.


Linkers combines these object files together to generate
executable object file.

Backing store:

Is a part of hard disk/secondary memory that is used by a paging or


swapping system to store data not currently in main memory?

Paging:

is the act of transferring pages between physical memory &


backing store.

Page table:

In a virtual memory system, mapping between virtual address &


physical address is done by means of a data structure called page
table. A page table contains address of the page in main memory &
current status of the page.

Page fault:

Page fault is an access to a page that has been paged out (i.e., not
in memory). This is an exception during translation of virtual
address to physical address.

Page:

Block of fixed size as a unit of paging.

Page frame:

An area in memory that can hold one page is called page frame.

129
Note: Pages & page frames will be ofthe same sii.e

CPU can access main memory & registers directly. Instructions which
are in execution & data used by instructions should be in these direct
access memory devices. If they are in secondary storage device, they
should be brought into primary memory before CPU uses it. Memory
access may involve several clocks cycles. & some cycles may have to be
stalled due to the unavailability of data.

Protection of memory has to be ensured by CPU hardware. If a program


executing in user mode tries to access operating system memory a trap
will be generated. Binding of instructions & data to memory address will
happen in any of the following steps.

1. Compile time
2. Load time
3. Execution time
Address generated by CPU is known as logical address and the address
seen by the CPU is known as physical address.

The set of logical addresses generated by the program is called logical


address space.

The set of physical addresses corresponding to these logical addresses is


a physical address space.

Mapping from virtual address to physical address is achieved by MMU.

3.9 Swapping

Swapping is a mechanism in which a process can be removed out of


main memory & moved to secondary storage (backing store) and make
that memory available to other processes. At some later time, the
system swaps back the process from the secondary storage to main
memory when it is needed.

130
The total time taken by swapping process includes the time it takes to
move the entire process to a secondary disk and then to copy the
process back to memory, as well as the time the process takes to regain
mammemory.

Swap In

Malp Swap out

Secon ary
Memory
Swapping

Figure 3.4 Concept of Swapping

3.10 Contiguous Memory Allocation

The first fit, best fit and worst fit are the strategies used to select a free
hole from the set of available holes.

Best fit: Allocate the smallest hole that is big enough. The allocator
places a process in the smallest block of unallocated memory in which it
will fit. For example, suppose a process requests 12KB of memory and
the memory manager currently has a list of unallocated blocks of 6KB,
14KB, 19KB, 11KB, and 13KB blocks. The best-fit strategy will allocate
12KB of the 13KB block to the process.

131
Worst fit: Allocate the largest hole.The memory manager places a
process in the largest block of unallocated memory available. The idea is
that this placement will create the largest hole after the allocations, thus
increasing the possibility that compared to best fit; another process can
use the remaining space. Using the same example as above, worst fit will
allocate 12KB of the 19KB block to the process, leaving a 7KB block for
future use.

First fit: Allocate the first hole that is big enough. There may be many
holes in the memory, so the operating system, to reduce the amount of
time it spends analyzing the available spaces, begins at the start of
primary memory and allocates memory from the first hole it encounters
large enough to satisfy the request. Using the same example as above,
first fit will allocate 12KB of the 14KB block to the process.

"111111

rta.

laof<J N

Problem
Given five memory partitions of l00Kb, 500Kb, 200Kb, 300Kb,
600Kb (in order), how would the first-fit, best-fit, and worst-fit
algorithms place processes of 212 Kb, 417 Kb, 112 Kb, and 426 Kb
(in order)? Which algorithm makes the most efficient use of
memory?

First-fit:
212K is put in SOOK partition
417K is put in 6OOK partition
112K is put in 288K partition (new partition 288K = SOOK - 212K)
132
426K must wait

Best-fit:
212K is put in 300K partition
417K is put in 500K partition
112K is put in 200K partition
426K is put in 600K partition

Worst-fit:
212K is put in 600K partition
417K is put in 500K partition
112K is put in 388K partition
426K must wait

In this example, best-fit turns out to be the best.

Problem
Consider a paging system with the page table stored in memory.
a. If a memory reference takes 200 nanoseconds, how long does a
paged memory reference take?
b. If we add associative registers, and 75 percent of all page-table
references are found in the associative registers, what is the effective
memory reference time? (Assume that finding a page-table entry in
the associative registers takes zero time if the entry is there.)

Answer:
a. 400 nanoseconds: 200 nanoseconds to access the page table and 200
nanoseconds to access the word in memory.

b. Effective access time = 0.75 x (200 nanoseconds) + 0.25 x


(400nanoseconds) = 250 nanoseconds .

133
External & internal fragmentation

Free hole 4k

Allocated

Free hole 4k

Allocated

Allocated

Allocated

Free hole 4k

Consider the above figure, there are 3 free holes of 4K and total free size
is 12K. Suppose we have to allocate a process of 12K we will not be able
to allocate it even though we have 12K free. This problem is called
external fragmentation.

External fragmentation can be avoided with the technique called


compaction where all free holes are combined together to form a large
hole.

Internal fragmentation: Memory block assigned to process is bigger.


Some portion of memory is left unused, as it cannot be used by another
process.

3.11 Paging

Processor generates virtual address/logical address which are divided


into page no. & an offset. Page no is added with the starting address of
the page table to obtain the starting address of the Page (p) which wi11
be in page table. Page table contains control bits which indicate

134
whether the page is in main memory or it has been modified or any
restrictions to use the page. The starting address of the page is
combined with offset to get physical address.

t.ogicr. I r:idd1'C!Ko

l'iipn•I .. rd ... •
f.i.oih
I 11 Q(l',...I _
ltl(:t-~ 1f
~ . .11ne
~drl-cs; o1
t.'".C P..'IC 1---- +
•~h""

I
r~ ~,3~~ I
..:t!.r~·~ NO-~ I
ru~ •:,❖

Figure 3.5 Paging Structure

If a program has to get executed it has to use the physical memory. Some
operating systems utilize extra memory then the available physical
memory. This extra memory is the secondary memory which is called as
virtual memory.

Some programs are large enough to be placed into physical memory. So


only the portion of the program which is required to be executed should
be provided with main memory & the rest of the program which are not
currently being executed are stored in secondary memory, when they
need to get executed they are brought back to the main memory. So there
is a need for the movement of programs and data between main memory
& secondary memory. This will be taken care by virtual memory system.

Here you can perceive an advantage i.e., the programmer does not need
to worry about physical memory available for his program to execute.
Memory management unit which is a hardware unit translates virtual
address to physical address.

135
Page table should reside in memory management unit. But memory
management unit which belongs to processor chip is not large enough to
keep page table. So page table will be placed in main memory & some
portion of page table is kept in memory management unit.

Overlays
The program is divided into several modules. If any module is required
to be executed, then at that point of time that module is loaded. Suppose
if any other module requires being loaded then those modules which do
not require main memory at that point of time are overwritten & the new
module is loaded. In case if the old module which was under execution
earlier has been altered then it should be brought back to secondary
memory before overwriting.

Translation look-aside buffer (TLB)

l _ _,:-;-:- 7. T•' • -
•t"r- l

L -

Figure 3.6 Paging Hardware with TLB

When a logical address is generated by the CPU, its page number is fed
into the TLB. If the page number is found (called TLB hit), its frame
number is used to access the memory.

136
If the page number is not in TLB (called TLB miss), page table must be
referred. When the frame number is generated, it is used to access the
memory.

3.12 Structure of Page Table

3.12.1 Hierarchical Paging

In Hierarchical paging we deal with two-level paging algorithm where


page table itself is paged. Consider 32-bit machine with page size of
4KB. Logical address is divided into page number of 22 bits & an offset
of 10 bits. Since we page the page table the page number is further
divided into page number of 12 bits & an offset of 10 bits.

Pagernumber Page offset

P1 P2 d

12 10 10

I Pl I Pl I d

- -

n
Outer
Page uf
EJ
Page 'Paae T'~bl.I
Tat,le

Figure 3.7 Address Translation

137
Pl is the index of the outer page table & P2 is the offset of the outer page
table.

Address translation is shown in the fig 3.7.

Consider a 32-bit machine with page size of 512 bytes. The address
looks like this.

I S (2 bits) IP
(21 bits) I d (9 bits)
S represents section number. (2 bits)

P represents logical page number of that section. (21 bits)

d represents offset. (9 bits)

For 64-bit logical address space two-level paging scheme is not an


appropriate choice. Suppose if the page size is 4KB & the page table
consists of 252 entries & if two-level paging scheme is used the address
looks like this.

Pl (42) P2 (10) d (12)


Outer Page Inner Page Offset

The outer page table consists of 242 entries. So we need to divide the
outer page table. Then we have to page the outer page table which leads
to three-level paging scheme.

3.12.2 Hashed Page Tables

Hashed page tables can be used for address space of more than 32-bits.
Hash value is the page number. Each entry in the hash table contains a
linked list of elements. Each element contains

1) Virtual page number


2) Value of the mapped page frame
3) Pointer to the next element in the linked list.

138
Virtual page number is hashed to the hash table. Virtual page number is
compared with the I st field of the first element in the linked list. In case if
there is a match, the 2 nd field (Page frame) is used to form the physical
address. If there is no match subsequent entries are searched.

l'hysrc■I .iutch-ss

r
" rt

Mt!lflllfY

Figure 3.8 Hashed Page Table

Inverted Page Table

'l
:.,,.td
• ti i '

Figure 3.9 Inverted Page Table

139
An inverted page table has one entry for each frame. Each entry consists
of virtual address of the page. There is only one page table in the system.
It has only one entry for each page. In this system an address space
identifier i.e., process id is required which will be stored in each entry of
the page table. Each virtual address consists of

1. Process id
2. Page number
3. Offset.

Each entry in the inverted page table is the pair Process id & Page
number. When a memory reference occurs this pair is considered &
inverted page table is searched for matching. If there is a match at ith
entry then physical address <i, offset > is generated.

This scheme reduces the memory needed to store each page table. In
this scheme searching time is more.

3.13 Segmentation

First of all we have to know what user's view of memory is. It is a


collection of segments. Segments may not be of the same size & there is
no ordering of the segments. Segmentation supports user's view of
memory. The logical address is a collection of segments. The logical
address consists of segment number & offset within the segment.
Segments are generated by the compiler.

We need to map two dimensional addresses to one dimensional sequence


of bytes with the help of segment table. Each entry in the segment table
contains segment base & segment limit.

Segment base: Starting address of the physical address where segment is


stored in memory.

Segment limit: Length of the segment.

140
Logical address consist segment number & offset. Segment number is
the index into the segment table. Offset should be between O & segment
limit. If offset is not less than the segment limit then addressing error will
occur. If offset is valid then it is added with the segment base to fonn the
physical address.

<Jllllnt lll lliM1'

t-- 1i1 - hr.._ •


li11---t 7
1J r1,o:ul
r.\ ..,~,1...,,.1

- - \:; - ·
'1.'1i:'i1J1I Jfto

.Jdd.c[IJ,
DC

Figure 3.10 Segmentation Hardware

141
Chapter 3: Review Questions
1. Explain Deadlock Characterization.
2. Write methods for handling deadlock
3. How do you prevent deadlock
4. How do you avoid deadlock
5. Explain banker's algorithm with an example
6. How do you detect deadlock
7. How do you recover from deadlock
8. Explain Contiguous memory Allocation
9. Explain the concept of paging
10. Define page & frame.
11. Define overlays
12. Explain Translation Look Aside Buffer with a neat diagram
13. Explain different structures of page table
14. Write a note on segmentation
15. Write differences between paging & segmentation

142

You might also like