Sie sind auf Seite 1von 38

Virtual Memory in Unix

Idan Shatz
Unix’s Method
 Ready?
The Big Picture
A process
Swap Device
0 Page 0
Page 1 0 block 0
Page 2 block 1
Page 3 block 2
Main (Physical) Memory
0 Frame 0
4GB Page n Frame 1
Frame 2
A process Page in
0 Page 0 Page out
Page 1 128MB Frame m
Page 2
Page 3

1GB block p
4GB Page n
The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page D.B.D. block 1
Table Page 2
Table Page D.B.D.
PageTable
3
Table Table
Table block 2
Main (Physical) Memory
Page
Page D.B.D.
D.B.D.
0 Frame 0 Swap-use
PFdata Swap-use
Table Table
Table Frame PFdata
1
4GB Page Table
n Table
Table
Table
Frame 2Table
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m
Page D.B.D.
Table Page 2Page
Table D.B.D.
Page Table
3Table Table
Table
Page
Page D.B.D.
D.B.D.
Table Table 1GB block p
4GB Page nTable Table
Tables
 Region table
 Text, Data, Stack (per process)
 Page table
 Virtual  Physical translation (per process)
 Disk Block Descriptor (DBD) table
 Disk location of swapped pages (per process)
 PFData (Page Frame) table
 Description of physical frame status (singleton)
 Swap in-use table
 The same as PFData, but for the swap file
(singleton)
Process Memory

process address space


Text

static allocation
Data
dynamic allocation

Stack
4GB
Region Table

0
Region table
Text

process address space


Data Text
Stack

static allocation
Data
dynamic allocation
Start Region
Permissions
Address Size

Stack
4GB
Pages and Frames
Virtual Memory
0 Main (Physical) Memory

0
process address space

Text

static allocation
Data
16MB
dynamic allocation

Frame (4k)

Stack
Page
4GB
Page Table
page table
Page 0
region table Page 1
Text Page 2
Data …
Stack Page N-2
Page N-1

Start Region
Permissions
Address Size

Valid Physical
Permissions Ref Mod COW Age
Flag Address
The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page block 1
Table Page 2
Table Page
PageTable
3
Table block 2
Main (Physical) Memory
Page
Page 0 Frame 0
Table Frame 1
4GB Page Table
n
Frame 2
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m
Page
Table Page 2Page
Table
Page Table
3Table
Page
Page
Table 1GB block p
4GB Page nTable
Translation from Virtual to Physical address
Virtual Address
region table
Text page table
Data
Page 0
Stack
Page 1
Page 2

StartTranslation
Region (Boooo)
Permissions Page N-2
Address Size
Page N-1

Valid Physical
Permissions Ref Mod COW Age
Flag Address

Physical Address
Translation from Virtual to Physical address
Virtual Address offset
region table
Text page table
= Data
Page 0
Stack
Page 1
Page 2

Start Region
Permissions Page N-2
Address Size
Page N-1

Valid Physical
Permissions Ref Mod COW Age
Flag Address

Frame Number offset


Translation from Virtual to Physical address
Virtual Address offset
region table
Text page table
Data The Calculation:
Page 0
Stack
Page 1(Virtual – Start)/Page Size
Page 2

Start Region
Permissions Page N-2
Address Size
Page N-1

Valid Physical
Permissions Ref Mod COW Age
Flag Address

Page Number offset


Translation from Virtual to Physical address
Virtual Address offset
region table
Text page table
Data
Page 0
Stack
Page 1
Page 2

Start Region
Permissions Page N-2
Address Size
Page N-1

Valid Physical
Permissions Ref Mod COW Age
Flag Address

Page Number offset


The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page block 1
Table Page 2
Table Page
PageTable
3
Table block 2
Main (Physical) Memory
Page
Page 0 Frame 0
Table PFdata
4GB Page Table
n Frame PFdata
1
Table
Frame 2Table
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m
Page
Table Page 2Page
Table
Page Table
3Table
Page
Page
Table 1GB block p
4GB Page nTable
Physical Memory & Pfdata table

Pfdata table Main (Physical) Memory

16MB

Frame (4k)
Ref Swap/ Swap Block
Status
Count File Device Number
The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page block 1
Table Page 2
Table Page
PageTable
3
Table block 2
Main (Physical) Memory
Page
Page 0 Frame 0 Swap-use
PFdata Swap-use
Table Frame PFdata
1
4GB Page Table
n Table
Table
Table
Frame 2Table
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m
Page
Table Page 2Page
Table
Page Table
3Table
Page
Page
Table 1GB block p
4GB Page nTable
Swap Device & swap use

Swap use table Swap device

1GB

Block (4k)

Ref Count
The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page D.B.D. block 1
Table Page 2
Table Page D.B.D.
PageTable
3
Table Table
Table block 2
Main (Physical) Memory
Page
Page D.B.D.
D.B.D.
0 Frame 0 Swap-use
PFdata Swap-use
Table Table
Table Frame PFdata
1
4GB Page Table
n Table
Table
Table
Frame 2Table
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m
Page D.B.D.
Table Page 2Page
Table D.B.D.
Page Table
3Table Table
Table
Page
Page D.B.D.
D.B.D.
Table Table 1GB block p
4GB Page nTable Table
Virtual & Swap connection
region table Disk block
descriptor
Text page table table
Data
Page 0 Page 0
Stack
Page 1 Page 1
Page 2 Page 2
… …
Start Region
Permissions Page N-2 Page N-2
Address Size
Page N-1 Page N-1

Valid Physical
Permissions Ref Mod COW Age
Flag Address

Swap/ Swap Block Fill 0/


File Device Number Demand fill
Example
Virtual Address Page Table Entry Disk Block Descriptor

Swap Dev. 1
1493K Page No. 794
Block No. 2743

Page Frame 794


Swap Use
Ref Cnt 1

Swap Dev. 1 Red Cnt 1


Block No. 2743

Physical Page 794 Swap Dev. Block 2743


Agenda
 Page Faults
 Ref, Mod, COW and Age
 Page In/Out Method
 Pager Process
 Page-Stealer Process
 System Calls
 Exec
 Fork
Page Faults
 3 Types of page Faults
 Page Faults
 Protection Fault
 Segmentation Fault
Page Faults
 3 Types of page Faults
 Page Faults
 Happens when valid=0.
 Protection Fault
 Happens on permission violation
 Segmentation Fault
 Happens when virtual address is illegal.
Page Faults
 3 Types of page Faults
 Page Faults
 Happens when valid=0.
 Causes pages in only (How?)
 Protection Fault
 Happens on permission violation
 Good Example: Writing to a read-only page
 Segmentation Fault
 Happens when virtual address is illegal.
Ref & Mod
page table
Page 0
Page 1
Page 2 Updated by CPU:
… • Ref – set to 1 when the process
Page N-2 accesses a page.
Page N-1
• Mod – set to 1 when the process
modifies a page

Valid Physical
Permissions Ref Mod COW Age
Flag Address
COW & Age
page table
Page 0
Page 1
Page 2 The other two, updated by O.S.
… • COW – Copy on Write Flag
Page N-2 The page will be copied
on first write (Fork)
Page N-1
• Age – Page Age (a number)
=0 – to warm pages
>0 – to cold pages

Valid Physical
Permissions Ref Mod COW Age
Flag Address
The Unix Big Picture
A process
Swap Device
0 Page 0
0 block 0
Region Page 1
Region
Page D.B.D. block 1
Table Page 2
Table Page D.B.D.
PageTable
3
Table Table
Table block 2
Main (Physical) Memory
Page
Page D.B.D.
D.B.D.
0 Frame 0 Swap-use
PFdata Swap-use
Table Table
Table Frame PFdata
1
4GB Page Table
n Table
Table
Table
Frame 2Table Pager
Pager
A process Page in
0 Page 0 Page out
Region Page 1
Region 128MB Frame m Page-stealer
Page-stealer
Page D.B.D.
Table Page 2Page
Table D.B.D.
Page Table
3Table Table
Table
Page
Page D.B.D.
D.B.D.
Table Table 1GB block p
4GB Page nTable Table
Pager
 Loads Pages into main memory.
 Usually, the Pager process is asleep.
 Wakes up on a Page Fault (Type 1).
Pager
 The Page In process:
1. Look at the D.B.D table to find the page at
the swap.
2. Look at the Pfdata table to check if the
page is found at the main memory.
a. Skip to 4
3. Load the page from swap to main memory
4. Update tables.
When Valid=0
Virtual Address offset

region table Disk block


descriptor
Text page table table
Data
Page 0 Page 0
Stack
Page 1 Page 1
Page 2 Page 2
Find the swap block
… …
number from the disk
Page N-2 Page N-2
block descriptor table
Page N-1 Page N-1

Valid Physical
....
Flag Address

Swap/ Swap Block Fill 0/


File Device Number Demand fill
When Valid=0

Pfdata table Main (Physical) Memory

Before reading the 0


block from the swap,
Unix tries to find the
page in the main
memory.
Pfdata has a hash for
speeding up the
search process 16MB

Frame (4k)
Ref Swap/ Swap Block
Status
Count File Device Number
Page Stealer
 The Bad Guy 
 Wakes up when the
number of free frames
passes the low-water
mark of free frames
 Start to free frames till
High
the high-water mark
Low
Which page to throw?
 Remember the Locality Rule?
 Throw cold pages
 The Aging process
 Happens every second
 Update Age field
 If (REF = =0) Age ++;
 Else Age=0;
 Turns off REF field
Exec
 Create a new virtual memory
 New Region table
 New Page Tables & D.B.D
Fork
 Duplicate the virtual memory
 Text – can be shared because it is read-
only
 Data & Stack – need to be duplicated
 The Problem:
 Forking in order to do exec…
 Duplicate on Demand
 Share while you can…
 Make a copy only when needed!
Copy on Demand
 A page at the data region can be shared
until one of the processes modifies it.
 Therefore, the O.S. needs to trace the
first write to the page
 Make the page Read-only.
 Turn on the COW flag.
 When we have Protection Fault
 Check if the COW is on, if so, make a
duplicate.
Fork
 Text Region
 Share the page table
 Data & Stack
 Copy the page table.
 To all valid pages: Read-only & COW
 Protection Fault
 If COW is on
 copy the page
 Turn off COW.
 Change back to Read-Write permissions.

Das könnte Ihnen auch gefallen