22 Module 5 Introduction 13-06-2023
22 Module 5 Introduction 13-06-2023
UNIT - 3
Roadmap
• Background
• Swapping
• Basic requirements of Memory
Management.
• Memory Partitioning
• Basic blocks of memory management
—Paging
—Segmentation
— Segmentation with Paging
Background
• Program must be brought into memory and placed within a
process for it to be run.
Operating
System
1024
User Program
Unused
Multi-programming
Memory Partitioning
• Increase processor
utilization
• To take advantage of
multiprogramming, several
processes must reside in
memory
Memory Management Requirements
• Relocation
• Protection
• Sharing
• Logical organization
• Physical organization
Requirements: Relocation
• Fixed Partitioning
• Dynamic Partitioning
• Simple Paging
• Simple Segmentation
• Virtual Memory Paging
• Virtual Memory Segmentation
Fixed Partitioning( IBM mainframe Os & OS/MFT
(Multiprogramming with fixed no of Tasks )
Overlay Area
Unused
Solution – Unequal Sized Fixed Partitions
• Equal-size
—Placement is trivial (no options)
• Unequal-size
—Can assign each process to the smallest
partition within which it will fit
—Queue for each partition
—Processes are assigned in such a way as to
minimize wasted memory within a partition(
Internal fragmentation )
Fixed Partitioning
Remaining Problems with Fixed
Partitions
OS (8M)
• External Fragmentation
• Memory external to all
P2
P1
(14M)
processes is fragmented
(20M)
Empty (6M)
• Can resolve using
compaction
Empty
P4(8M)
P2
(56M)
(14M) —OS moves processes so that
Empty (6M)
they are contiguous
P3 —Time consuming and wastes
(18M) CPU time
Empty (4M)
• Best-fit algorithm
—Chooses the block that is closest in size to the
request
—Worst performer overall
—Since smallest block is found for process, the
smallest amount of fragmentation is left
—Memory compaction must be done more often
Dynamic Partitioning
• First-fit algorithm
—Scans memory form the beginning and
chooses the first available block that is large
enough
—Fastest
—May have many process loaded in the front
end of memory that must be searched over
when trying to find a free block
Dynamic Partitioning
• Next-fit
—Scans memory from the location of the last
placement
—More often allocate a block of memory at the
end of memory where the largest block is
found
—The largest block of memory is broken up into
smaller blocks
—Compaction is required to obtain a large block
at the end of memory
Dynamic Partitioning
• Worst-fit
—Chooses the block that is largest in size to the
request
—Leaves the largest leftover hole
Allocation
Buddy System
1M
128
AK 128 K 256 K 512 K
Example of Buddy System
Tree Representation of Buddy
System
Relocation
• Logical
—Reference to a memory location independent
of the current assignment of data to memory.
• Relative
—Address expressed as a location relative to
some known point.
• Physical or Absolute
—The absolute address or actual location in main
memory.
Relocation
Registers Used during Execution
• Base register
—Starting address for the process
• Bounds register
—Ending location of the process
• These values are set when the process is
loaded or when the process is swapped in
Registers Used during Execution
A.0
0---
13
7
4 000
A.1
A.2 1---
14
8
5 111
A.3 2---
9
6 222
Free frame list
D.0
B.0 33
10
11
3
D.1
B.1
12 4
D.2
B.2
ProcessCAB
Process
Process
C.0 pagetable
page table
table
Process
page D
C.1 page table
C.2
C.3
D.3
D.4
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.
10 10 12
Two-Level Page-Table Scheme
Address-Translation Scheme
• Address-translation scheme for a two-
level 32-bit paging architecture
Hashed Page Tables
• Common in address spaces > 32 bits.
Term Description
Frame Fixed-length block of main
memory.
• Frame Locking
—If frame is locked, it may not be replaced
—Kernel of the operating system
—Key control structures
—I/O buffers
—Associate a lock bit with each frame
Basic Replacement
Algorithms
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
• FIFO Replacement – Belady’s Anomaly
—more frames ⇒ less page faults
FIFO Page Replacement
FIFO Illustrating Belady’s Anamoly
Optimal Algorithm
• Replace page that will not be used for
longest period of time.
• 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
2 6 page faults
3
4 5
• How do you know this?
• Used for measuring how well your
algorithm performs.
Optimal Page Replacement
Least Recently Used (LRU) Algorithm
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2,
3, 4, 5 1 5
2
3 5 4
• 4 3
• Counter implementation
—Every page entry has a counter; every time
page is referenced through this entry, copy
the clock into the counter.
—When a page needs to be changed, look at the
counters to determine which are to change.
LRU Page Replacement
LRU Algorithm (Cont.)
• Stack implementation – keep a stack of
page numbers in a double link form:
—Page referenced:
– move it to the top
– requires 6 pointers to be changed
—No search for replacement
Examples II