OS Unit - 4
OS Unit - 4
Page |1
MEMORY MANAGEMENT
REGISTERS
USING BASE AND LIMIT
HARDWARE ADRESS PROTECTION are only storage that CPU
can a c e s s
256000
process
300040
300040 eivtlACW base
M A
120900
420940 limit
process
880000
024000
OS memory or other users
ye
CPU
no no
Ameotion t Pa T deta th the actual p. Mbéoti
Dc ColleAB
RA
Corrpile ebcofob
P 6s
Page l2
changed values.
Execution time:
1. If process can be mobbed during its execution from one memory segment to another
then binding must be delayed until run time.
2. Specified h/w is required which will discuss further.
space.
7. Thus in execution time address binding scheme, logical & physical address space differ.
t ,1 rot
rot r mebo- uculion lime.
ur b chi q
M.N.Praneswara Rao, Dept. of CSE RGMCET,NDL
Page |3
time mapping form virtual to physical address is done by h/w device called MMU
Run
memory management unit). Mapping with simple MMU scheme.
in
Base register is called relocation register
-
a special register in CPU that helps
relocation of program it contains an integer value
The value in relocation register is added to every address generated by
user process at
time it is sent to memory.
EX relocated to
1. If base is at 14000 then attempt by user to address location is dynamically
location 14000.
2. DOS running on 8086 uses 4 relocation registers when loading & running processes.
3. User never sees real physical address.
4. User program deals with logical address.
address to physical address.
5. Memory mapping h/w converts logical
address (range 0 to max)
6. Now have tow types of addresses: logical
max value, R->base value)
Physical address(R+0 to R+
that process runs in location 0 to max.
7. User generates logical address & thinks
instruction or data byte as used in process.
8. Logical address is address of an
or segment register.
9 This address is obtained using index, base
10. Physical address is effective memory address
=
of aninstruction or data byte.
relocation
register
14000
physical
logical address
address
memory
CPU 14346
346
MMU
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
Page l4
SWAPPING
PPORttine
awap out
a w a p in
backing atoro
main memor
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
Page f6
Contiguous memory allocation
CPU
no no
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
Page |8
EX: size
order), place the processes ;of
For thepartitions of 100k,500k,200k,300k,600k i(in
the first bit, best fit and worst fit algorithms
212k,417k,112k,426k(in order) according
Free partitions 100k,500k,200k,30ok,600k
First Fit:
We allocate the first process in the first available partition large enough to
accommodate first process
(i.e
partition 1 of size 500k leaving 288k memory hole
as
P1 of size 212k gets allocated in
500k-212k=288k)
Free partitions 100k,288k,200k,300k,600k
(i.e
partition 4 of size 600k leaving 183k memory hole
as
P2 of size 417k gets allocated in
600k-417k=183k)
Free partitions 100k,288k,200k,300k,183k
partition 2 of size 200k leaving 88k memory as hole (i.e
P3 of size 112k gets allocated in
200k-112k=88k) 83
Free partitions 100k,288k,88k,300k,6©ek
hence it will wait until
P4 of size 426k will not find any free partitionto accommodate
either P2 or P1 is swapped out from the memory
Fragmentation is
from memory, the
free memory space
AS processes are loaded & removed
broken into little pieces. for the
total memory space
exists when there is enough
External fragmentation (storage is fragmented
into large no.
request but the spaces are not contiguous
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
PM- fomess
LA Pege
LA - Paeno 0 e
no
wng Pno CoHespen'kinq ore Page t10
PAGING avnids exterarenta n
to be noncontiguous.
1. This permits physical address space of process
a
OSS
2. Paging is most commonly used in most
BASIC METHOD
3. Breaking physical memory into fixed
size blocks called frames
of same size called pages.
4. Breaking logical memory into blocks available memory
are loaded into
S. When process is to be executed, its pages
frames from backing store.
frames
blocks=size of the memory
6. Backing store is divided into fixed size
into 2 parts page number (p) & page offset
7. Address generated by CPU is divided ilge
lkatan
(d)
8. Page numberis used as indexto a e i Hoh
page tablememorylieitcontans a
contains base address ofeach page in physical
Page table with offset to define physical memory
address
10. Base address is combined page
sent to memory unit
logical physical
foo00.. 0000
address
addre
CPU
1 1 1 1 . . 1111
physical
memory
Dage table
pago 1 Pago
pago 2
P a g o aa
page table 3Pago2
ogioanl
4 page 1
ITYemory
page
phyical
memory
pao« ablo
28
PhYolcoAI m M ry
EX:
72766 bytes would need 35 pages+ 1086
If page size is 2048 bytes a process job
frames resulting internal fragmentation of 2048-
bytes. It would be allocated 36
1086-962 bytes
pages would need n pages plus byte
case
1
In worst
It would allocate n+1 frames, resulting
in internal fragmentation of almost an
entire frame
larger;
EX
Solaris support 4 KB & 8 MB (multiple page size)
Each page table entry is 4 bytes entries can address 244 bytes (16 TB) of
physical memory
26. Each page of process needs one frame
27. If process requires n pages, at least n frames must be available in memory
28. If n frames are available they are allocated to this arriving process
29. First page of process is loaded into one of allocated frames; frame number is put
in page table
30. Next page is loaded into another frame and its frame number i_ put into page
table
free-frame list free-frame list
14 15
13
20 14 14
15
15 15
6
Peige 16
Pege
17 Rage 17
page 3
new process 18 new procosS 18 pace
19 19
o1
20 20 ag
320
new-process page table 21
(a) (b)
page frar
number nOnb
TLB miss_
physICal
memoy
Page table
reference is used.
11. If page is not in TLB then it is called TLB miss then memory reference to page
table is made
it to access memory.
12. When frame number is obtained we can use
13. Then we add page number & frame number
to TLB, so it can be found quickly in
next reference.
one for replacement.
14. If TLB is full of entries the OS must select
15. Replacement policies must range from LRU (least recently used) to random.
220ns
effective access time
b)Considering hit ratio of 80% & 98% what is the
nanoseconds
Effective access time=0.80*120+0.20*220=140
nanoseconds
access time from 100 to 140
We suffer 40% slow down in memory
For 98% hit ratio we have
time=0.98*120+0.20*220=122 nanoseconds
Effective access
in time.
produced only 22% slow down
access
Increased hit ratio
B)PROTECTION
1. Protection in paged environment
2. Protection bits is associated with each frame
3. These bits are kept in page table.
or read-only
4. One bit define a page to be read-write
can frame
to fine correct
reference to memory goes through the page table
.tvery
number. made to read
that no writes are being
6. Protection bits can be checked to verify
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
he kax
distonce thot patevlas eerent
t-The Ceament home
bae
Te engh ef Poxficules in PM
amit ad o boe Pantu bhg
BRspec fes the stating Page |16
pede3
Page
7 UC
10,468
2.
pay® lablo
Page
SEGMENTATION
and an offset.
rather than
6. Segments are numbered and are referred to by a segment number,
consists of two tuple:
by a segment name. Thus, a logical address
a
i. <segment-number, offset>
7. Each entry of the segment table has a segment base and
a segment limit.
limit base
segment
table
CPU
yes
no
that segment, d.
index into the segment table.
10. The segment number is used as an
address must be between 0 and the segment limit.
11. The offset d of the logical
M.N.Praneswara Rao, Dept. of CSE RGMCET,NDL
Page |17
5700
6300
6700 menti
physical memorv
the beginning
16. The segment table has a separate entry for each segment, giving
and the length of that
address of the segment in physical memory (or base)
segment (or limit).
location 4300.
17. For example, segment 2 is 400 bytes long and beginsis at location 4300
18. 253. indicates a reférence to byte 53 of segment 2 mapped onto
+53=4353.
to 3200 (the base
Indicates a reference to segment 3, byte 852, mapped
is
19. 3852.
ofsegment 3)+852= 4052.
20.01222, indicates a reference to 1222 of segment 0 would result in
byte a trap to
tod code
-
to en
Demand Paging
associated page is
this value indicates that the
1. When this bit is set to "valid,"
both legal and in memory.
either is not valid
this value indicates that the page
2. If the bit is set to "invalid," but is currently
address space of the process), or is valid
(that is, not in the logical
on the disk.
into is set as usual, but
page-table entry for a page that is brought
memory
3. The marked
for that is not currently in memory is simply
the page-table entry a page
on disk.
invalid, or contains the address of the page
vaid vald
bit
nonmory
pwalael memorv
execution time
4. First load entire program into memory at program
5. Second load pages only as they are needed
If the process tries to access a page that was not brought into memorycauses
6.
page-fault trap.
the page table, will
7. The paging hardware in translating the address through
to the operating system. This trap
notice that the invalid bit is set, causing a trap
failure to bring the desired page into
is the result of the operating system's
memory.
opering
Sysem
reorenc
pagetaDle
restart
instrucOn
bring
m i s s o pag"
physicul
memory
whether the reference was a
1.We check an internal table for this process, to determine
valid or invalid memory access.
terminate the process. If it was valid, but
we have not
2. If the reference was invalid, we
now page it in.
yet brought in that page, we list.
one from the free-frame
3. We finda free frame by taking
M.N.Praneswara Rao, Dept. of CSE RGMCET,NDL
Page |19
.We schedule a disk operation to read the desired page into the newly allocated
frame.
S. When the disk read is complete, we modify the internal table kept with the process
and the page table to indicate that the page is now in memory.
6. We restart the instruction that was interrupted by the illegal address trap.
been in memory.
ne process can now access the page as though it had always
Performance of Demand paging
. With demand page virtual memory, pagers are only loaded when they are
occurs
7. Able to restart exactly same place & state
8. IF 1D OF, if error at IF restart by fetching instruction again
& then fetch
9. If error at OF then again we must fetch & decode instruction
operand
10. EX: Consider an instruction
a. Fetch and decode the instruction (ADD).
b. Fetch A.
c Fetch B.
d. Add A and B.
e. Store the sum in C
C is in page which is not in
11. If page fault when we are storing inC (because
memory) again,
fetch instruction, decoding
12. Then we need to restart the instruction from
fetching two operands adding them
13. Difference:
several different locations
14. Only when one instruction modifies
from one location to other
15. Move instruction which moves 256 bytes
done
16. If page fault occurs when move is partially in
blocks overlap source may have been modified,
17. Also the source & destination
which we can't simply restart the instruction
2 ways
1. Access both ends of both blocks.
If page fault is going to occur it will happen before anything is modified
ate stored in memory
Move can then take page fault occur, since
place, no all pages
hold values of ovenwritten locations.
2. Temporary register to
written back into memory before trap occurs
If page fault, al old values are
state before instruction was started, so that instruction
Action restores memory to its
can be repeated.
Page Replacement
1. If no frame is free, we find one that is not currently being
used and free it.
2. We can free a frame by writing its contents to swap space and changing the page
table (and all other tables) to indicate that the page is no longer in memory
3. Page-fault service routine to include page replacement:
a. Find the location of the desired page on the disk.
b. Find a free frame:
1. If there is a free frame, use it.
to
2. If there is no free frame, use a page-replacement algorithm
select a victim frame.
frame
3. Write the victim frame to the disk; change the page and
tables accordingly.
C. Read the desired page into the newly freed frame; change the page and
frame tables.
d. Restart the user process.
This
4. If no frames are free, two page transfers (one out and one in) are required.
situation effectively doubles the page-fault service time and increases the
the page is written into, indicating that the page has been modified.
8. When we select a page for replacement, we examine its modify bit.
9. If the bit is set, we know that the page has been modified since it was read in
is already there.
12. This technique also applies to read-only pages (for example, pages of binary
code)
13. Such pages cannot be modified; thus, they may be discarded when desired.
Page-Replacement algorithm
1. There are many different page-replacement algorithms. How do we select a
particular replacement algorithm? In general, we want the one with the lowest
page-fault rate.
2. Evaluate an algorithm by running it on a particular string of memory references
and computing the number of page faults. The string of memory references is
called a reference string.
3. For a given page size (and the page size is generally fixed by the hardware or
system), we need to consider only the page number.
4. Second, if we have a reference to a page p, then any immediately following
references to page p will never cause a page fault. Page p will be in memory after
the first reference, so the immediately following references will not fault.
M.N.Praneswara Rao, Dept. of CSE RGMCET,NDL
Page |21
of the queue.
reference string
701 2 0 30 4 2 3 0
3 21 2 0 1 7 0 1
page frames
and are brought into these
(7,0,1) cause page faults
4. The first three references
empty frames.
7 was brought in first.
5. Next reference (2) replaces page 7, because page we have no faut for this
6. 0 is the next reference and:0 is already in memory,
reference
0
7. First reference to 3 results in replacement of page
8. Page 1 is then replaced by page 0
9. There are 15 faults altogether
10. Its performance is not alwaysgood. rate and slows process
increases the page-fault
11. A bad replacement choice
execution a FIFO page-replacement
1,2,3,4,1,2,5,1,2,3,4,5
RGMCET,NDL
M.N.Praneswara Rao, Dept. of CSE
Page 122
ohronce sting
01 7 0
701 2 0/3 0 4 2 3 0. 3 2 2
ARge hames
3. The first three references cause faults that fill the three empty frames.
not be used until
4. The reference to page 2 page 7, because 7 will
replaces
at 14. The reference
reference 18, whereas page 0 will be used at 5, and page 1
to page 3 replaces page 1, as page 1 will be
the last of the three pages in memory
to be referenced again
5. Only nine page faults, optimal replacement is much better than a FIFO algorithm,
which resulted in fifteen faults.
must suffer, then optimal
Note: If we ignore the first three, which all algorithms
replacement is twice as good as FIFO replacement. it
6. The optimal page-replacement algorithm
is difficult to implement, because
encountered a similar
future knowledge of the reference string. (We
requires
situation with the SJF CPU-scheduling algorithm
LRU Page Replacement
for the longest period of time. This
1. replace the page that has not been used
approach is the least-recently-used (LRU) algorithm.
LRU replacement associates with each page
the time of that page's last use.
2.
been used for
3. When pagea be replaced, LRU chooses the page that has not
must
the longest period of time
elerence sting
0 3 2 1 2 0 1 7 0
7 01 2 0 3 0 4 23
page frames
4. LRU algorithm produces 12 faults
5. First 5 faults are the same as those for optimal replacement
4 occurs, however, LRU replacement sees that, of
6. When the reference to page
least recently. Thus, the LRU
the three frames in memory, page 2 was used
2 is about to be used.
algorithm replaces page 2, not knowing that page
M.N.Praneswara Rao, Dept. of CSE RGMCET,NDL
Page |23
it is now the
7. It then faults for page 2, the LRU algorithm replaces page 3; since
least recently used of the three pages in memory
An LRU page-replacement algorithm may require substantial hardware assistance
REPLACEMENT
COUNTING BASED PAGE
of refferences that have been made
to page
1. Counter for countingthe number
2. LFU: page with smallest count to be replaced
refference count
Actively used page should have large
initial phase but is never used later
Problem: page is heavily used during
remains in memory even though it is no
Because the page that has heavy count,
longer used
the use count
Sol: shift the count right by 1 bit at regular intervals which delays
probably just brought in and has yet to be used
page with smallest count
was
3 MFU:
a)Additiona-Reference-Bits Algorithm
bits at regularintervals
1. Additional ordering by recording refference
2. Keep 8 bit byte for each page in a table in memory
transfered to OS
3. At regular intervals (100ms)control will be
4. OS shifts reference bits for each page into higher order bit,
shifting other to right
5. 8 bit shift register contains history of page for last eight time periods
6. If it contains 00000000it is not used for the last eight timeperiod
7. If it contains 11111111 it is used atleast once in each period
can be replaced
8. Page with lowest number is LRUpage and it
b]Second-Chance Algorithm
1. Basic algorithm is FIFO
2. When page is selected its refference bits are inspected ifit is 0 we proceed to replace
3. If it is 1 we give a second-chance and move on to select the next FIFO page
time is reset to
4. If a page gets second-chance its refference bit is cleared and arrival
Current time
5. Page given second-chance will not be replaced untill all other pages have been replaced
orgiven second-chance
6. way to implement is circular.queue
7. A pointer indicates which page to be replaced next
8. When frame is needed pointer advances untill it finds a page with a 0 refference bit
9. As it advances it clears reference bits
10. Once victim pageis found the page is replaced and the new page is inserted
11. Worst case when all bits are set, pointer goes through the whole queue giving each page
a second-chance
c)ENHANCEDSECOND-CHANCE ALGORITHM
1. considering 2 bits refference bit and modify bit as an ordered pair
2. 4 possible classes
(0,0)-neither recently used nor modified-best page to replace
(0,1)- not recently used but modified- not good, bcoz page need to written on to
disk before replacement
(1,0)-recently used but clean-may be used soon
(1,1)- recently used and modified -will be used agin and page will need to be
written on to disk before replacement