Title: Chapter 9: Virtual Memory
1Chapter 9 Virtual Memory
2Chapter 9 Virtual Memory
- Background
- Demand Paging
- Copy-on-Write
- Page Replacement
- Allocation of Frames
- Thrashing
- Memory-Mapped Files
- Allocating Kernel Memory
- Other Considerations
- Operating-System Examples
3Objectives
- To describe the benefits of a virtual memory
system - To explain the concepts of demand paging,
page-replacement algorithms, and allocation of
page frames - To discuss the principle of the working-set model
4Background
- Virtual memory separation of user logical
memory from physical memory. - Only part of the program needs to be in memory
for execution - Logical address space can therefore be much
larger than physical address space - Allows address spaces to be shared by several
processes - Allows for more efficient process creation
- Virtual memory can be implemented via
- Demand paging
- Demand segmentation
5Virtual Memory That is Larger Than Physical Memory
?
6Virtual-address Space
7Shared Library Using Virtual Memory
8Demand Paging
- Bring a page into memory only when it is needed
- Less I/O needed
- Less memory needed
- Faster response
- More users
- Page is needed ? reference to it
- invalid reference ? abort
- not-in-memory ? bring to memory
- Lazy swapper never swaps a page into memory
unless page will be needed - Swapper that deals with pages is a pager
9Transfer of a Paged Memory to Contiguous Disk
Space
10Valid-Invalid Bit
- With each page table entry a validinvalid bit is
associated(v ? in-memory, i ? not-in-memory) - Initially validinvalid bit is set to i on all
entries - Example of a page table snapshot
- During address translation, if validinvalid bit
in page table entry is I ? page fault
Frame
valid-invalid bit
v
v
v
v
i
.
i
i
page table
11Page Table When Some Pages Are Not in Main Memory
12Page Fault
- If there is a reference to a page, first
reference to that page will trap to operating
system - page fault
- Operating system looks at another table to
decide - Invalid reference ? abort
- Just not in memory
- Get empty frame
- Swap page into frame
- Reset tables
- Set validation bit v
- Restart the instruction that caused the page fault
13Page Fault (Cont.)
- Restart instruction
- block move
- auto increment/decrement location
14Steps in Handling a Page Fault
15Performance of Demand Paging
- Page Fault Rate 0 ? p ? 1.0
- if p 0 no page faults
- if p 1, every reference is a fault
- Effective Access Time (EAT)
- EAT (1 p) x memory access
- p (page fault overhead
- swap page out
- swap page in
- restart overhead
-
)
16Demand Paging Example
- Memory access time 200 nanoseconds
- Average page-fault service time 8 milliseconds
- EAT (1 p) x 200 p (8 milliseconds)
- (1 p) x 200 p x 8,000,000
- 200 p x 7,999,800
- If one access out of 1,000 (ie. p 0.001) causes
a page fault, then - EAT 8.2 microseconds.
- This is a slowdown by a factor of 40!!
17Process Creation
- Virtual memory allows other benefits during
process creation - - Copy-on-Write
- - Memory-Mapped Files (later)
18Copy-on-Write
- Copy-on-Write (COW) allows both parent and child
processes to initially share the same pages in
memoryIf either process modifies a shared page,
only then is the page copied - COW allows more efficient process creation as
only modified pages are copied - Free pages are allocated from a pool of
zeroed-out pages
19What happens if there is no free frame?
- Page replacement find some page in memory, but
not really in use, swap it out - algorithm
- performance want an algorithm which will result
in minimum number of page faults - Same page may be brought into memory several times
20Page Replacement
- Prevent over-allocation of memory by modifying
page-fault service routine to include page
replacement - Use modify (dirty) bit to reduce overhead of page
transfers only modified pages are written to
disk - Page replacement completes separation between
logical memory and physical memory large
virtual memory can be provided on a smaller
physical memory
21Need For Page Replacement
22Basic Page Replacement
- Find the location of the desired page on disk
- Find a free frame - If there is a free
frame, use it - If there is no free frame,
use a page replacement algorithm to select a
victim frame - Bring the desired page into the (newly) free
frame update the page and frame tables - Restart the process
23Page Replacement
24Page Replacement Algorithms
- Want lowest page-fault rate
- Evaluate algorithm by running it on a particular
string of memory references (reference string)
and computing the number of page faults on that
string - In all our examples, the reference string is
-
- 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
25Graph of Page Faults Versus The Number of Frames
26First-In-First-Out (FIFO) Algorithm
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - 3 frames (3 pages can be in memory at a time per
process) -
- 4 frames
-
- Beladys Anomaly more frames ? more page faults
1
1
4
5
2
2
1
3
9 page faults
3
3
2
4
1
1
5
4
2
2
1
10 page faults
5
3
3
2
4
4
3
27FIFO Page Replacement
28FIFO Illustrating Beladys Anomaly
29Optimal 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
- How do you know this? MUST CONSIDER THE FUTURE!
- Used for measuring how well your algorithm
performs
1
4
2
6 page faults
3
4
5
30Optimal 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
- How do you know this?
- Used for measuring how well your algorithm
performs
1
4
Choose to replace 4 by 5 since the time until 4
will be used is the longest.
2
6 page faults
3
4
5
31Optimal 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
- How do you know this?
- Used for measuring how well your algorithm
performs
1
4
2
6 page faults
Since 1 (and 2, 3) is not included in the future
schedule, assume it is complete and the frame is
available to reload 4.
3
4
5
32Optimal Page Replacement
33Least Recently Used (LRU) Algorithm
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - 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
1
1
5
1
1
2
2
2
2
2
5
4
4
3
5
3
3
3
4
4
34Least Recently Used (LRU) Algorithm
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - 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
1
1
5
1
1
2
2
2
2
2
At the time 5 is entered, we most recently used
2, 1 and 4. Hence, 3 was least recently used and
is replaced.
5
4
4
3
5
3
3
3
4
4
35LRU Page Replacement
36LRU 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
37Use Of A Stack to Record The Most Recent Page
References
38LRU Approximation Algorithms
- Reference bit
- With each page associate a bit, initially 0
- When page is referenced bit set to 1
- Replace the one which is 0 (if one exists)
- We do not know the order, however
- Second chance
- Need reference bit
- Clock replacement
- If page to be replaced (in clock order) has
reference bit 1 then - set reference bit 0
- leave page in memory
- replace next page (in clock order), subject to
same rules
39Thrashing
- If a process does not have enough pages, the
page-fault rate is very high. This leads to - low CPU utilization
- operating system thinks that it needs to increase
the degree of multiprogramming - another process added to the system
- Thrashing ? a process is busy swapping pages in
and out
40Thrashing (Cont.)
41Demand Paging and Thrashing
- Why does demand paging work?Locality model
- Process migrates from one locality to another
- Localities may overlap
- Why does thrashing occur?? size of locality gt
total memory size
42Working-Set Model
- ? ? working-set window ? a fixed number of page
references Example 10,000 instruction - WSSi (working set of Process Pi) total number
of pages referenced in the most recent ? (varies
in time) - if ? too small will not encompass entire locality
- if ? too large will encompass several localities
- if ? ? ? will encompass entire program
- D ? WSSi ? total demand frames
- if D gt m ? Thrashing
- Policy if D gt m, then suspend one of the processes
43Working-set model
44Keeping Track of the Working Set
- Approximate with interval timer a reference bit
- Example ? 10,000
- Timer interrupts after every 5000 time units
- Keep in memory 2 bits for each page
- Whenever a timer interrupts copy and sets the
values of all reference bits to 0 - If one of the bits in memory 1 ? page in
working set - Why is this not completely accurate?
- Improvement 10 bits and interrupt every 1000
time units
45Page-Fault Frequency Scheme
- Establish acceptable page-fault rate
- If actual rate too low, process loses frame
- If actual rate too high, process gains frame
46Buddy System
- Allocates memory from fixed-size segment
consisting of physically-contiguous pages - Memory allocated using power-of-2 allocator
- Satisfies requests in units sized as power of 2
- Request rounded up to next highest power of 2
- When smaller allocation needed than is available,
current chunk split into two buddies of
next-lower power of 2 - Continue until appropriate sized chunk available
47Buddy System Allocator
48End of Chapter 9
49Chapter 10 File-System Interface
50Chapter 10 File-System Interface
- File Concept
- Access Methods
- Directory Structure
- File-System Mounting
- File Sharing
- Protection
51Objectives
- To explain the function of file systems
- To describe the interfaces to file systems
- To discuss file-system design tradeoffs,
including access methods, file sharing, file
locking, and directory structures - To explore file-system protection
52File Concept
- Contiguous logical address space
- Types
- Data
- numeric
- character
- binary
- Program
53File Structure
- None - sequence of words, bytes
- Simple record structure
- Lines
- Fixed length
- Variable length
- Complex Structures
- Formatted document
- Relocatable load file
- Can simulate last two with first method by
inserting appropriate control characters - Who decides
- Operating system
- Program
54File Attributes
- Name only information kept in human-readable
form - Identifier unique tag (number) identifies file
within file system - Type needed for systems that support different
types - Location pointer to file location on device
- Size current file size
- Protection controls who can do reading,
writing, executing - Time, date, and user identification data for
protection, security, and usage monitoring - Information about files are kept in the directory
structure, which is maintained on the disk
55File Operations
- File is an abstract data type
- Create
- Write
- Read
- Reposition within file
- Delete
- Truncate
- Open(Fi) search the directory structure on disk
for entry Fi, and move the content of entry to
memory - Close (Fi) move the content of entry Fi in
memory to directory structure on disk
56Open Files
- Several pieces of data are needed to manage open
files - File pointer pointer to last read/write
location, per process that has the file open - File-open count counter of number of times a
file is open to allow removal of data from
open-file table when last processes closes it - Disk location of the file cache of data access
information - Access rights per-process access mode information
57File Types Name, Extension
58Access Methods
- Sequential Access
- read next
- write next
- reset
- no read after last write
- (rewrite)
- Direct Access
- read n
- write n
- position to n
- read next
- write next
- rewrite n
- n relative block number
59Sequential-access File
60Simulation of Sequential Access on a
Direct-access File
61Example of Index and Relative Files
62Directory Structure
- A collection of nodes containing information
about all files
Directory
Files
F 1
F 2
F 3
F 4
F n
Both the directory structure and the files reside
on disk Backups of these two structures are kept
on tapes
63A Typical File-system Organization
64Operations Performed on Directory
- Search for a file
- Create a file
- Delete a file
- List a directory
- Rename a file
- Traverse the file system
65Organize the Directory (Logically) to Obtain
- Efficiency locating a file quickly
- Naming convenient to users
- Two users can have same name for different files
- The same file can have several different names
- Grouping logical grouping of files by
properties, (e.g., all Java programs, all games,
)
66Single-Level Directory
- A single directory for all users
Naming problem Grouping problem
67Two-Level Directory
- Separate directory for each user
- Path name
- Can have the same file name for different user
- Efficient searching
- No grouping capability
68Tree-Structured Directories
69Tree-Structured Directories (Cont)
- Efficient searching
- Grouping Capability
- Current directory (working directory)
- cd /spell/mail/prog
- type list
70Tree-Structured Directories (Cont)
- Absolute or relative path name
- Creating a new file is done in current directory
- Delete a file
- rm ltfile-namegt
- Creating a new subdirectory is done in current
directory - mkdir ltdir-namegt
- Example if in current directory /mail
- mkdir count
mail
prog
copy
prt
exp
count
Deleting mail ? deleting the entire subtree
rooted by mail
71File Sharing
- Sharing of files on multi-user systems is
desirable - Sharing may be done through a protection scheme
- On distributed systems, files may be shared
across a network - Network File System (NFS) is a common distributed
file-sharing method
72File Sharing Multiple Users
- User IDs identify users, allowing permissions and
protections to be per-user - Group IDs allow users to be in groups, permitting
group access rights
73Protection
- File owner/creator should be able to control
- what can be done
- by whom
- Types of access
- Read
- Write
- Execute
- Append
- Delete
- List
74Access Lists and Groups
- Mode of access read, write, execute
- Three classes of users
- RWX
- a) owner access 7 ? 1 1 1 RWX
- b) group access 6 ? 1 1 0
- RWX
- c) public access 1 ? 0 0 1
- Ask manager to create a group (unique name), say
G, and add some users to the group. - For a particular file (say game) or subdirectory,
define an appropriate access.
owner
group
public
chmod
761
game
Attach a group to a file chgrp G
game
75A Sample UNIX Directory Listing
76End of Chapter 10