Professional Documents
Culture Documents
Đỗ Quốc Huy
huydq@soict.hust.edu.vn
Bộ môn Khoa Học Máy Tính
Viện Công Nghệ Thông Tin và Truyền Thông
Chap 3 Memory Management
①Introduction
②Memory management strategies
③Virtual memory
④Memory management in Intel’s processors family
Chapter 3 Memory management
1. Introduction
lExample
lMemory and program
lAddress binding
lProgram’s structures
Chapter 3 Memory management
1. Introduction
1.1 Example
C compilation process
Chapter 3 Memory management
1. Introduction
1.1 Example
C compilation process
Chapter 3 Memory management
1. Introduction
1.1 Example
C compilation process
Chapter 3 Memory management
1. Introduction
1.1 Example
C compilation process
Chapter 3 Memory management
1. Introduction
1.1 Example
C compilation process
Chapter 3 Memory management
1. Introduction
1.1 Example
Example: Generate programs from multi moduls
Toto project
Result
KQ: 1000
Chapter 3 Memory management
1. Introduction
1.1 Example
toto project compilation process
lExample
lMemory and program
lAddress binding
lProgram’s structures
Chapter 3 Memory management
1. Introduction
1.2. Memory and program
Memory leveling
l Memory is an important system’s resource
l Program must be kept inside memory to run
Memory
lExample
lMemory and program
lAddress binding
lProgram’s structures
Chapter 3 Memory management
1. Introduction
1.3. Address binding
Application program processing steps
Object
Dynamic link
Com Link
-pile Modul
Executive Program
Fetch
Modul inside
memory
System’s
library Main memory
Chapter 3 Memory management
1. Introduction
1.3. Address binding
Types of address
l Symbolic
l Name of object in the source program
l Example: counter, x, y,...
l Relative address
l Generated from symbolic address by compiler
l Relative position of an object from the module’s first position
l Example: Byte number 10 from the begin of module
l Absolute address
l Generate from relative address when program is loaded into memory
l For IBM PC: relative address <Seg :Ofs>→ Seg * 16+Ofs
l Object’s address in physical memory – physical address
l Example: JMP 010Ah ⇒ jump to the memory block at 010Ah at the same code
segment (CS)
l if CS=1555h, jump to location: 1555h*10h+010Ah =1560Ah
Chapter 3 Memory management
1. Introduction
1.3. Address binding
Physical address-logic address
Logic Physic
address address
Memory
Address register
l Logic address
l Generate from process, (CPU brings out)
l Converted to physical address when access to object in the program by the
Memory management unit (MMU)
l Physical address
l Address of an element (byte/word) in main memory
l Correspond to the logic address bring out by CPU
l Program work with logical address
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
lExample
lMemory and program
lAddress binding
lProgram’s structures
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
① Linear structure
④ Overlays structure
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Linear structure
M0
M1
Linker M0 M1 M2 M3
M2
M3
l Disadvantages
l Waste of memory
l Not all parts of the program are necessary for the
program’s execution
l It’s not possible to run the program that larger than physical
memory’s size
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Dynamic loading structure
M0 Operating System
M1
M2
M3
M0 Operating System
M1 M0
M2
M3
M0 Operating System
M1 M0
M1
M2
M3
M0 Operating System
M1 M0
M1
M2
M2
M3
M0 Operating System
M1 M0
M2 M1
M2
M3
M0 Operating System
M1 M0
M2 M3
M3
Advantage
l Can use memory are smaller than the program’s size
l High memory usage effectiveness if program is managed well
Disadvantage
l Slow when execution
l Mistake may cause waste of memory and increase execution time
l Require user to load and remove modules
l User must understand clearly about the system
l Reduce the program’s flexible
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Dynamic-link structure
toto()
M
l Links will be postponed when program is
executing
l Part of the code segment (stub) is utilized
Operating system
to search for corresponding function in the
library in the memory toto()
l When found, stub will be replace by the
address of the function and function will be
toto
executed
l Useful for constructing library
M
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
l Modules are divided into different levels
l Level 0 contains main modul, load and localize the program
l Level 1 contains modules called from level 0’s module and these modules
do not exist at the same time
l ...
l Memory is also divided into levels corresponding to program’s levels
l Size equal to the same level’s largest module’s size
l Overlay structure requires extra information
l Program is divided into how many levels, which modules are in each levels
l Information is stored in a file (overlay map)
l Module at level 0 is edited into an independent executable file
l When program is executed
l Load level 0 module similar to a linear structure program
l When another module is needed, load that module into corresponding
memory’s level
l If there are module in the same level exist, bring that module out
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
Main
memory
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
80K
120K
Main
Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M1
80K
120K
Main
Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M1
80K
M11
120K
Main
Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M1
80K
M11
120K
Main
M11 Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M1
80K
M12
120K
Main
Bộ nhớ trong
memory
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M1
80K
120K
M12
Main
M1 Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure
M0
80K
M2
M
80K
1
M12
120K
Main
Bộmemory
nhớ trong
Chapter 3 Memory management
1. Introduction
1.4. Program’s structures
Overlays structure - Conclusion
l Allow program with larger size than memory area size
allocated by the operating system
l Require extra information from user
l Effectiveness is depend on provided information
l Memory usage effectiveness is depend on how program’s
modules are organized
l If there are exist modules that larger than other modules
in the same level ⇒ the effective is reduced
l Module loading process is dynamic but program’s structure is
static ⇒ not change at each time running
l Provide more memory, the effectiveness does not increase
Chap 3 Memory Management
①Introduction
②Memory management strategies
③Virtual memory
④Memory management in Intel’s processors family
Chapter 3 Memory management
2. Memory management strategies
0 0
Operating Operating
system system
400 400
?
Process Size time
P1 600 10
P2 1000 5
P3 300 20
P4 700 8
P5 500 15
Waiting file queue
2560 2560
Chapter 3 Memory management
2. Memory management strategies
2.2 Dynamic partition strategy
Free memory area selection strategy
main program
5th instruction data
1st member
stack
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Logical address OS
space
subroutine array
Seg 2
Seg3
main program
data
Segment 0
stack
Seg4
Segment1
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
OS
Logical address
space 1400
subroutine array
Seg 2
Seg3 Segment 0
main program
data
Segment 0
stack
Seg4
Segment1 4300
Seg 3
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
OS
Logical address
space 1400
5x4
subroutine array
5th instruction 1420
Seg 2
Seg3
main program
data Seg 0
Segment 0
stack
Seg4 4300
Segment1
11x2
11th member 4322
Seg 3
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Segmentation structure
l Program is a combination of modul/segment
l Segment number, segment’s length
l Each segment can be edited independently.
l Compile and edit program -> create SCB (Segement Control Block)
l Each member of SCB is corresponding to a program’s segment
OS
Logical address
space
subroutine array
Seg 2
Seg3
main program
data M A L
Segment 0
0 - 1000
stack 0 - 400
Seg4 0 - 400
Segment1 0 - 1100
0 - 1000
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
OS
Logical address
space
1400
subroutine array Seg 0
2400
Seg 2
3200
Seg 3
main program
Seg 3 4300
data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 0 - 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <3,345> = ?
OS
Logical address
space
1400
subroutine array Seg 0
345 2400
Seg 2 member
3200
Seg 3
main program Offset 345 3545
Seg 3 4300
data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 0 - 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <4,185> = 4885
?
OS
Logical address
space
1400
subroutine array Seg 0
2400
Seg 2
3200
Seg 3
main program
Seg 3 4300
data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 0 - 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <2,120> = ?
OS
Logical address
space
1400
subroutine array Seg 0
2400
Seg 2
3200
Seg 3
main program
Seg 3 4300
data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 0 - 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <2,120> = ?
OS
Logical address
space
1400
subroutine array Seg 0
2400
2800
Seg 2 Seg 2
3200
Seg 3
main program
Seg 3 4300
Jmp <2:120> data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 0 - 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <2,120> = ?
OS
Logical address
space
1400
subroutine array Seg 0
2400
2800
Seg 2 Seg 2
3200
Seg 3
main program
Seg 3 4300
Jmp <2:120> data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 1 2800 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <2,120> = 2920
OS
Logical address
space
1400
subroutine array Seg 0
2400
2800
Seg 2 Seg 2
3200
Seg 3
main program
Seg 3 4300
Jmp <2:120> data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 1 2800 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Example
Address <2,450> = ?
OS
Logical address Access error!
space
1400
subroutine array Seg 0
2400
2800
Seg 2 Seg 2
3200
Seg 3
main program
Seg 3 4300
Jmp <2:120> data M A L
Seg 0
1 1400 1000 4700
stack 1 6300 400 Seg 4
Seg 4 1 2800 400 5700
Seg 1 1 3200 1100 6300
1 4700 1000 Seg 1
6700
SCB
Memory
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Address conversion: memory accessing diagram
No
Access Access error
error Yes
Memory
N
Logic address o
Physical
address
Segment s
Segment Control Block
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Conclusion: pros
l Module loading diagram does not require user ‘s participation
l Easy to protect segments
l Check memory accessing error
l Invalid address : more than segment’s length
l Check accessing’s property
l Code segment: read only -> Write into code segment: accessing error
l Check the right to access module
l Add accessing right (user/system) into SCB
l Allow segment sharing (Example: Text editor)
Process 2
Data S3
S0 (Read only)
sqrt()
S0
Process 1
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Segment sharing: Main problem
l Sharing segment
l Call (0, 120) ?
l Read (1, 245) ?
=>must has the same index
number in the SCB
Chapter 3 Memory management
2. Memory management strategies
2.3 Segmentation strategy
Conclude: cons
0 5
g 1 6
2 1
3 2
PCB
g
Logical memory
Access the logic address [ 6 ] ?
Address [ 6 ]: Page 1, displacement 2 Physical
Address <1,2> = 6*4 + 2 = 26 (624) memory
Chapter 3 Memory management
2. Memory management strategies
2.4 Paging strategy
Program is running → Load program into memory
Memory
Logic address
Physical
address
page f
l 40 user
l No sharing
l Need 8000K
l Sharing
l Require 2150K
Chapter 3 Memory management
2. Memory management strategies
2.4 Paging strategy
Page sharing: rule
Logic address
Memory
Physical
address
Segment Control Block Page f
Page Control Block
Chapter 3 Memory management
2. Memory management strategies
2. 5 Segmentation and paging combination strategy
Conclusion
M A L M A
M A L 0 −
0 − 3 1 8
0 − 2340
0 5 6 0 −
1 2140 5730
0 − 5 0 −
0 − 4264 0 −
0 − 2
0 − 1766 0 −
SCB SCB
PCB2
Chap 3 Memory Management
①Introduction
②Memory management strategies
③Virtual memory
④Memory management in Intel’s processors family
Chapter 3: Memory management
3. Virtual memory
3.1 Introduction
①Introduction
l Process’s page:
l Physical memory,
l Some pages stay
on disk (virtual
memory)
l Represented by one
bit in the PCB
l When a page is
required, load page
from secondary
memory -> physical
memory
Chapter 3: Memory management
3. Virtual memory
3.1 Introduction
Page fault handling
① Determine location
of logical page on
disk
② Select physical frame
l Write to disk
l Modify bit valid-
invalid
③ Load logic page into
selected physical
frame
④ Restart process
Chapter 3: Memory management
3. Virtual memory
3.1 Introduction
①Introduction
l ...
Chapter 3: Memory management
3. Virtual memory
3.2 Page replacement strategies
FIFO
Example
Remark
l Effective when the program has the linear structure
l Least effective when the program has different modules call
l Easy to implement
l Utilize a queue to store program’s pages inside memory
l Insert into last position in the queue, replace page at the first
position
l Increase physical page, no guarantee of reducing page fault
l Accessing sequence: 1 2 3 4 1 2 5 1 2 3 4 5
l 3 frames: 9 page faults; 4 frames: 10 page faults
Chapter 3: Memory management
3. Virtual memory
OPTreplacement strategies
3.2 Page
OPT
Rule: Replace page that has longest next used time
Use counter (one field of PCB) to record the last time page is
accessed
l LFU: Page that has smallest counter will be replaced
l Page that is frequently used
l Important page ⇒ reasonable
l Initialization page, only used at start ⇒ unreasonable ⇒
Shift right the counter by 1 bit (time div 2)
l MFU: Replace page with largest counter
l Page with smallest value, just recently loaded and not used
much
Chap 3 Memory Management
①Introduction
②Memory management strategies
③Virtual memory
④Memory management in Intel’s processors family
Chapter 3: Memory management
4. Memory management in Intel’s processors family