OS Mod 3 by ?
OS Mod 3 by ?
OS Mod 3 by ?
Deadlock
3.1 System model:
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
n2. • • •
Rl1 •••
Fl3 • •
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
Process states
P2is holding the resource type ~ & waiting for resource type R2.
94
Figure 3.2 Resource allocation graph with a deadlock.
95
3.3 Methods for handling deadlock
Mutual Exclusion:
For e.g. A read only file can be shared by more than are process.
96
Both the approaches have two disadvantages
No Preemption:
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.
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.
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.
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
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
99
Bankers Algorithm -Problems
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
Need
A B C
Pl 3 1 0
P2 1 2 0
P3 0 2 1
We have to add the available matrix with the resources released by P2.
P2 was allocated <0,3, l >(Look at allocation matrix).
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.
101
we can satisfy Pl's need, since Pl need<3,l,0><= Currently available
<3,5,3>.
Now all the processes were allocated resources & they executed. The
system is in safe state. Safe sequence is P2,P3,Pl.
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?
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<=Available
Need<=A vailable
103
Need<=Available
Need< Available
Need<=A vailable
104
<O O 2 0 ><= <3 8 8 6> is true .
Need<=Available
105
We have to do following modification & then apply safety algorithm
New Snapshot
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
106
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2
Need<=Available
107
Available Matrix is < I I I 2>
Need<=A vailable
Need<=A vailable
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
If an additional request from P2 arrives for <l O 2> can the request
be granted immediately?
Answer:
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<=A vailable
111
So we cannot allocate resources to P3 at this point of time
112
Now P 1 & P3 are pending
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)
If an additional request from P2 arrives for <1 0 2> can the request
be granted immediately?
Answer
=<3 0 2>
114
=< l 2 2> - < l O2>
=<O 2 O>
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
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
Is true
So allocate resources to P2
Let us consider P3
Let us consider P4
Need <=Available
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)
Let us consider Pl
This request cannot be granted at this point of time because we may not
get safe sequence.
Let us consider P3
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)
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
118
Answer the following questions using Bankers Algorithm.
Let us consider PO
119
Let us consider Pl
Let us consider P2
Let us consider 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
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:
121
2. Is request for P2 <= Available?
<OO2><= <l O2> is true.
Following modifications are made
Available = Available-Request
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
Let us Consider PO
Let us consider PI
Need <= Available
Let us consider P2
123
Let us consider P3
So allocate resources to P3
Let us consider P4
So allocate resources to P4
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
So allocate resources to P 1
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
Need = Max-Allocation
R1 R2 R3
Pl 1 4 5
P2 2 3 0
P3 2 2 0
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.
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 -
~: -
Process termination
Resource preemption:
Linking:
Loading:
Static linking:
Dynamic linking:
128
Symbol resolution:
Backing store:
Paging:
Page table:
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:
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.
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.
3.9 Swapping
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
Secon ary
Memory
Swapping
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
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.
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.
3.11 Paging
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~ •:,❖
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.
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.
l _ _,:-;-:- 7. T•' • -
•t"r- l
L -
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.
P1 P2 d
12 10 10
I Pl I Pl I d
- -
n
Outer
Page uf
EJ
Page 'Paae T'~bl.I
Tat,le
137
Pl is the index of the outer page table & P2 is the offset of the outer page
table.
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)
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.
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
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
'l
:.,,.td
• ti i '
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
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.
- - \:; - ·
'1.'1i:'i1J1I Jfto
.Jdd.c[IJ,
DC
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