0% found this document useful (0 votes)
63 views24 pages

Memory: Memory Management (General Principles)

Uploaded by

Ankur Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views24 pages

Memory: Memory Management (General Principles)

Uploaded by

Ankur Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Memory

Memory Management
(General Principles)
Memory Management
• Objectives
– User believes he is the unique user of the all space.
– User’s address space is infinite.
• Realities
– Many users share the memory.
– A program consists of modules written by many.
– Primary memory has limited size.
– Second memory is 1 to 2 order cheaper than p.m.

12/08/21 Operating Systems Memory Manag 2


ement
Multi-step Processing of a User
Program
• Linking : unifying addresses from different modules
• Binding : changing addresses in user’s program
to real ones logical address ---> physical address
• Loading: putting the program into memory
• Executing the program

Should linking, binding, and loading be always done in this order

12/08/21 Operating Systems Memory Manag 3


ement
When to Bind ?
• During compile time
– Need to know the precise memory allocation
– Only work for small simple programs
• During load time
– The program cannot be moved around during execution
– Simple but inconvenient
• During execution time
– The program can be moved around during execution
– Run-time overhead => hardware assistance

12/08/21 Operating Systems Memory Manag 4


ement
Overlays
• Enhancing memory availability
• Better having dynamic loading and binding
Overlay driver
Frequently used routines
Main Memory

Routine A Overlay driver


Frequently used routines

Routine B

Application
12/08/21 Operating Systems Memory Manag 5
ement
Swap
• Why ?
• Who should be swapped?
• When ?

• Need of dynamic binding (and dynamic


loading)

12/08/21 Operating Systems Memory Manag 6


ement
Single-Partition Allocation
• Basic idea: allocate the entire memory to
ONE process
– Simple, minimum cost
– User has maximum flexibility
– No service from OS
– No multiprogramming

12/08/21 Operating Systems Memory Manag 7


ement
A better Idea
• Allocate the memory to O.S and one
process
– Some service from OS
– Still no multi-programming
– An issue : where to put O.S ?
O.S Answer : Put O.S in lower end :
Reasons:
- Interrupt vector
- memory expansions
User Program
& Data

12/08/21 Operating Systems Memory Manag 8


ement
Dynamic Binding Using
Relocation Register
Main
Base register 14000 Memory

Logical address Physical


CPU +
346 14346
address

12/08/21 Operating Systems Memory Manag 9


ement
Multiple-Partition Allocation
• Basic Idea: allocating memory to multiple processes
• Two ways :
– Fixed-size partition
• Memory is divided into several parts of fixed size
• Each process takes one part
• No longer in use
– Variable-size partition
• Memory is partitioned dynamically
• O.S. keeps a table of parts of memory available
• When a process arrives, OS searches for a part of
memory large enough for the process
12/08/21 Operating Systems Memory Manag 10
ement
Example

OS Job Queue
400K process mem. req. finish time
P1 600K 10
P2 1000K 5
P3 300K 20
2160K P4 700K 8
P5 500K 15

2560K
12/08/21 Operating Systems Memory Manag 11
ement
Example (cont’d)
OS OS OS OS OS
400K 400K 400K 400K 400K
P1 P1 P1 P5
1000K P2 Allocate P1 Allocate
terminates P4 terminates P5

P2 P4 P4 P4

2000K
2300K
P3 P3 P3 P3 P3
Job Queue
process mem. req. finish time
P1 600K 10
P2 1000K 5
P3 300K 20
P4 700K 8
P5 500K 15
12/08/21 Operating Systems Memory Manag 12
ement
Dynamic Allocation Problem
• Basic strategies:
– First-fit : allocate the first hole that is big enough.
– Best-fit: allocate the smallest hole that is big enough.
– Worst-fit: allocate the largest hole that is big enough
• Usually, first-fit is fastest and both first-fit
and best fit perform better than worst-fit.
• Any other problems? (To be discussed)
– Protection
– Fragmentation

12/08/21 Operating Systems Memory Manag 13


ement
Protection in Multiple-Partition
(Variable Size)
Limit register Base register Main
4096 14000 Memory

Physical
CPU Logical < +
346 14346
address
address

12/08/21 Operating Systems Memory Manag 14


ement
Fragmentation
The free memory space is broken into little pieces

• Internal fragmentation
– Definition : difference between required memory
and allocated memory.
– Cause:estimation error, management overhead.
• External fragmentation
– Definition : scattered small pieces not allocated
– Solution: compaction
– Compaction strategy
12/08/21 Operating Systems Memory Manag 15
ement
#t Ways to Compact Memory
OS OS OS OS
300K
P1 P1 P1 P1
500K
P2 P2 P2 P2
600K
P3
400K 800K P4
1000K 1000K 900K
P4
P3 P3
1200K
300K
1500K
900K 900K
P4 P4
1900K
200K P3
2100K
Original Moved 600K Moved 400K Moved 200K
12/08/21 Operating Systems Memory Manag 16
ement
Paging
• A compactingless memory management
– No external fragmentation

• A compromise between fixed-size partition


and variable-size partition: less internal
fragmentation than fixed-size partition

12/08/21 Operating Systems Memory Manag 17


ement
Paging (How)
• Memory is divided into frames of fixed small
size
• A process is allocated a number of frames as
it needs
• Implementation
– Divide logical memory into pages.
– A dynamic mapping (binding/relocation)
between pages and frames
12/08/21 Operating Systems Memory Manag 18
ement
Page # Frame#
0 a 0
Frame#
0 1 2
b
c
Page#
3 d 0 5 i
1
j
4 e 1 6 k

1 5 f l
2 1
6 g m
n
7 h 3 2 o 2
8 I p
2 9 10
j
k Page table 3
11 l
12 m
3 13 n
14 o 4
15 p
Logical Memory a
b 5
c
d
e
f
g 6
12/08/21 Operating Systems Memory Manag h 19
ement
Paging Hardware

Logical address Physical address Physical


Memory
CPU p d f d
Page table

12/08/21 Operating Systems Memory Manag 20


ement
Paged Segmentation
User’s view of memory

Subroutines
Main
Program

Stack

Data

12/08/21 Operating Systems Memory Manag 21


ement
Memory Management with
Segmentation
• Memory allocation
– Fixed-size partition
– Variable-size partition
• The problem still exists
– Paging: paged segmentation system

12/08/21 Operating Systems Memory Manag 22


ement
Paged Segmentation System
• Aim:taking the advantages of both paging and
segmentation system
• Implementation : treat each segment independently, each
segment has a page table
• Logical address consists of three parts:
– Segment#
– Page #
– Offset
• Dynamic address translation,

12/08/21 Operating Systems Memory Manag 23


ement
Summary of Chapter
• We mainly are concerned with these issues :
– Time
– Utilization of memory:fragmentation
– User convenience:sharing, segmentation
– Protection.

12/08/21 Operating Systems Memory Manag 24


ement

You might also like