OS Unit 5-Memory
OS Unit 5-Memory
OS Unit 5-Memory
Roadmap
• Basic requirements of Memory
Management
• Memory Partitioning
• Basic blocks of memory management
– Paging
– Segmentation
The need for memory
management
• Memory is cheap today, and getting
cheaper
– But applications are demanding more and
more memory, there is never enough!
• Memory Management, involves swapping
blocks of data from secondary storage.
• Memory I/O is slow compared to a CPU
– The OS must cleverly time the swapping to
maximise the CPU’s efficiency
Memory Management
Term Description
Frame Fixed-length block of main
memory.
Page Fixed-length block of data in
secondary memory (e.g. on disk).
Segment Variable-length block of data that
resides in secondary memory.
Addressing
Requirements: Protection
• Processes should not be able to reference
memory locations in another process
without permission
• Impossible to check absolute addresses at
compile time
• Must be checked at run time
Requirements: Sharing
• Allow several processes to access the
same portion of memory
• Better to allow each process access to the
same copy of the program rather than
have their own separate copy
Requirements: Logical
Organization
• Memory is organized linearly (usually)
• Programs are written in modules
– Modules can be written and compiled
independently
• Different degrees of protection given to
modules (read-only, execute-only)
• Share modules among processes
• Segmentation helps here
Requirements: Physical
Organization
• Cannot leave the programmer with the
responsibility to manage memory
• Memory available for a program plus its
data may be insufficient
– Overlaying allows various modules to be
assigned the same region of memory but is
time consuming to program
• Programmer does not know how much
space will be available
Partitioning
• An early method of managing memory
– Pre-virtual memory
– Not used much now
• But, it will clarify the later discussion of
virtual memory if we look first at
partitioning
– Virtual Memory has evolved from the
partitioning methods
Types of Partitioning
• Fixed Partitioning
• Dynamic Partitioning
• Simple Paging
• Simple Segmentation
• Virtual Memory Paging
• Virtual Memory Segmentation
Fixed Partitioning
• Equal-size partitions (see fig 7.3a)
– Any process whose size is less than
or equal to the partition size can be
loaded into an available partition
• The operating system can swap a
process out of a partition
– If none are in a ready or running
state
Fixed Partitioning Problems
• A program may not fit in a partition.
– The programmer must design the program
with overlays
• Main memory use is inefficient.
– Any program, no matter how small, occupies
an entire partition.
– This is results in internal fragmentation.
Solution – Unequal Size
Partitions
• Lessens both problems
– but doesn’t solve completely
• In Fig 7.3b,
– Programs up to 16M can be
accommodated without overlay
– Smaller programs can be placed in
smaller partitions, reducing internal
fragmentation
Dynamic Partitioning
• Partitions are of variable length and
number
• Process is allocated exactly as much
memory as required
Dynamic Partitioning
Example
OS (8M) • External Fragmentation
• Memory external to all
P2
P1
(14M)
(20M)
processes is fragmented
Empty (6M) • Can resolve using
Empty
P4(8M)
P2
(56M)
compaction
(14M)
Empty (6M)
– OS moves processes so
P3 that they are contiguous
(18M)
– Time consuming and
wastes CPU time
Empty (4M)
A.0
A.1
A.2
A.3
D.0
B.0
D.1
B.1
D.2
B.2
C.0
C.1
C.2
C.3
D.3
D.4
Page Table
Address Translation Scheme
• Address generated by CPU is divided into:
– Page number (p) – used as an index into a page table
which contains base address of each page in physical
memory
– Page offset (d) – combined with base address to define
the physical memory address that is sent to the memory
unit
page number page offset
p d
m -n n
Solution :
Given : 64 pages = 2^6 ; 1024 words = 2^10 ; 32 frames = 2^5
15 page faults
• Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
– Adding more frames can cause more page faults!
• Belady’s Anomaly
• How to track ages of pages?
– Just use a FIFO queue
7 7 7 7 3 3 3 3 2 2
0 0 0 0 4 4 4 4 7
1 1 1 1 0 0 0 0
2 h 2 h 2 h h 2 h h h h 1 1 h h 1h
h
Least Recently Used (LRU)
Algorithm
• Use past knowledge rather than future
• Replace page that has not been used in the most amount of
time
• Associate time of last use with each page