0% found this document useful (0 votes)
2 views88 pages

RTOS (Online Notes)

The document discusses the implementation and management of paging and virtual memory in operating systems, detailing the structure and function of page tables, TLBs, and memory protection mechanisms. It covers various paging techniques, including hierarchical and hashed page tables, as well as the inverted page table approach. Additionally, it provides examples related to Intel's IA-32 architecture and includes exercises and quizzes to reinforce the concepts presented.

Uploaded by

bodarukmini2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views88 pages

RTOS (Online Notes)

The document discusses the implementation and management of paging and virtual memory in operating systems, detailing the structure and function of page tables, TLBs, and memory protection mechanisms. It covers various paging techniques, including hierarchical and hashed page tables, as well as the inverted page table approach. Additionally, it provides examples related to Intel's IA-32 architecture and includes exercises and quizzes to reinforce the concepts presented.

Uploaded by

bodarukmini2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 88

Real-Time Operating

Systems M
7. Paging  Virtual Memory
Notice
The course material includes slides downloaded from:!
http://codex.cs.yale.edu/avi/os-book/!
(slides by Silberschatz, Galvin, and Gagne, associated with
Operating System Concepts, 9th Edition, Wiley, 2013)!
and!
http://retis.sssup.it/~giorgio/rts-MECS.html!
(slides by Buttazzo, associated with Hard Real-Time Computing
Systems, 3rd Edition, Springer, 2011)!
which has been edited to suit the needs of this course. !
The slides are authorized for personal use only. !
Any other use, redistribution, and any for profit sale of the slides (in any
form) requires the consent of the copyright owners.!

7.2! Torroni, Real-Time Operating Systems M ©2013!


Implementation of Page Table

 Page table is kept in main memory!

 Page-table base register (PTBR) points to the page table!

 Page-table length register (PTLR) indicates size of the page table!

 In this scheme every data/instruction access requires two memory accesses!


 One for the page table and one for the data / instruction!

 The two memory access problem can be solved by the use of a special fast-lookup hardware
cache called associative memory or translation look-aside buffers (TLBs)!

 Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely
identifies each process to provide address-space protection for that process!
 Otherwise need to flush at every context switch!

 TLBs typically small (64 to 1,024 entries)!

 On a TLB miss, value is loaded into the TLB for faster access next time!
 Replacement policies must be considered!
 Some entries can be wired down for permanent fast access!

Operating System Concepts – 9th Edition! 7.3! Silberschatz, Galvin and Gagne ©2013!
Associative Memory

 Associative memory – parallel search !

Page #! Frame #!

!!
!
 Address translation (p, d)!
 If p is in associative register, get frame # out!
 Otherwise get frame # from page table in memory!

Operating System Concepts – 9th Edition! 7.4! Silberschatz, Galvin and Gagne ©2013!
Paging Hardware With TLB

Operating System Concepts – 9th Edition! 7.5! Silberschatz, Galvin and Gagne ©2013!
Effective Access Time
 Associative Lookup = ε time unit!
 Can be < 10% of memory access time!
!
 Hit ratio = α!
 Hit ratio – percentage of times that a page number is found in the associative registers;
ratio related to number of associative registers!
!
 Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access!

 Effective Access Time (EAT)!


! !EAT = (1 + ε) α + (2 + ε)(1 – α)!
! ! != 2 + ε – α!

 Consider α = 80%, ε = 20ns for TLB search, 100ns for memory access!
 EAT = 0.80 x 120 + 0.20 x 220 = 140ns!

 Consider more realistic hit ratio -> α = 99%, ε = 20ns for TLB search, 100ns for memory
access!
 EAT = 0.99 x 120 + 0.01 x 220 = 121ns!

Operating System Concepts – 9th Edition! 7.6! Silberschatz, Galvin and Gagne ©2013!
Memory Protection

 Memory protection implemented by associating protection bit with each


frame to indicate if read-only or read-write access is allowed!
 Can also add more bits to indicate page execute-only, and so on
!
 Valid-invalid bit attached to each entry in the page table:!
 “valid” indicates that the associated page is in the process’ logical
address space, and is thus a legal page!
 “invalid” indicates that the page is not in the process’ logical
address space!
 Or use page-table length register (PTLR)!

 Any violations result in a trap to the kernel!

Operating System Concepts – 9th Edition! 7.7! Silberschatz, Galvin and Gagne ©2013!
Valid (v) or Invalid (i) Bit In A Page Table

Operating System Concepts – 9th Edition! 7.8! Silberschatz, Galvin and Gagne ©2013!
Shared Pages

 Shared code!
 One copy of read-only (reentrant) code shared among processes
(i.e., text editors, compilers, window systems)!
 Similar to multiple threads sharing the same process space!
 Also useful for interprocess communication if sharing of read-write
pages is allowed!

 Private code and data !


 Each process keeps a separate copy of the code and data!
 The pages for the private code and data can appear anywhere in
the logical address space!

Operating System Concepts – 9th Edition! 7.9! Silberschatz, Galvin and Gagne ©2013!
Shared Pages Example

Operating System Concepts – 9th Edition! 7.10! Silberschatz, Galvin and Gagne ©2013!
Structure of the Page Table

 Memory structures for paging can get huge using straight-forward methods!
 Consider a 32-bit logical address space as on modern computers!
 Page size of 4 KB (212)!
 Page table would have 1 million entries (232 / 212)!
 If each entry is 4 bytes -> 4 MB of physical address space / memory for
page table alone!
 That amount of memory used to cost a lot!
 Don’t want to allocate that contiguously in main memory!
!

 Hierarchical Paging!

 Hashed Page Tables!

 Inverted Page Tables!

Operating System Concepts – 9th Edition! 7.11! Silberschatz, Galvin and Gagne ©2013!
Hierarchical Page Tables

 Break up the logical address space into multiple page tables!

 A simple technique is a two-level page table!

 We then page the page table!

Operating System Concepts – 9th Edition! 7.12! Silberschatz, Galvin and Gagne ©2013!
Two-Level Page-Table Scheme

Operating System Concepts – 9th Edition! 7.13! Silberschatz, Galvin and Gagne ©2013!
Two-Level Paging Example

 A logical address (on 32-bit machine with 4K page size) is divided into:!
 a page number consisting of 20 bits!
 a page offset consisting of 12 bits!

 Since the page table is paged, the page number is further divided into:!
 a 10-bit page number !
 a 10-bit page offset!

 Thus, a logical address is as follows:

page number! page offset!

p 1! p 2! d!

! 10! 10! 12!

 where p1 is an index into the outer page table, and p2 is the displacement within
the page of the inner page table!
 Known as forward-mapped page table!

Operating System Concepts – 9th Edition! 7.14! Silberschatz, Galvin and Gagne ©2013!
Address-Translation Scheme

Operating System Concepts – 9th Edition! 7.15! Silberschatz, Galvin and Gagne ©2013!
64-bit Logical Address Space
 Even two-level paging scheme not sufficient!
 If page size is 4 KB (212)!
 Then page table has 252 entries!
 If two level scheme, inner page tables could be 210 4-byte entries!
 Address would look like!

outer page! inner page! page offset!

p 1! p 2! d!

42! 10! 12!

 Outer page table has 242 entries or 244 bytes!


 One solution is to add a 2nd outer page table!
 But in the following example the 2nd outer page table is still 234 bytes in size!
 And possibly 4 memory access to get to one physical memory location!

Operating System Concepts – 9th Edition! 7.16! Silberschatz, Galvin and Gagne ©2013!
Three-level Paging Scheme

Operating System Concepts – 9th Edition! 7.17! Silberschatz, Galvin and Gagne ©2013!
Hashed Page Tables

 Common in address spaces > 32 bits!

 The virtual page number is hashed into a page table!


 This page table contains a chain of elements hashing to the same location!

 Each element contains (1) the virtual page number (2) the value of the mapped
page frame (3) a pointer to the next element!

 Virtual page numbers are compared in this chain searching for a match!
 If a match is found, the corresponding physical frame is extracted!

 Variation for 64-bit addresses is clustered page tables!


 Similar to hashed but each entry refers to several pages (such as 16) rather
than 1!
 Especially useful for sparse address spaces (where memory references are
non-contiguous and scattered) !

Operating System Concepts – 9th Edition! 7.18! Silberschatz, Galvin and Gagne ©2013!
Hashed Page Table

Operating System Concepts – 9th Edition! 7.19! Silberschatz, Galvin and Gagne ©2013!
Inverted Page Table

 Rather than each process having a page table and keeping track of all possible logical
pages, track all physical pages!

 One entry for each real page of memory!

 Entry consists of the virtual address of the page stored in that real memory location, with
information about the process that owns that page!

 Decreases memory needed to store each page table, but increases time needed to
search the table when a page reference occurs!

 Use hash table to limit the search to one — or at most a few — page-table entries!
 TLB can accelerate access!

 But how to implement shared memory?!


 One mapping of a virtual address to the shared physical address!

Operating System Concepts – 9th Edition! 7.20! Silberschatz, Galvin and Gagne ©2013!
Inverted Page Table Architecture

Operating System Concepts – 9th Edition! 7.21! Silberschatz, Galvin and Gagne ©2013!
Example: Intel 32 Architecture

 Dominant industry chips!

 Pentium CPUs are 32-bit and called IA-32 architecture!

 Current Intel CPUs are 64-bit and called IA-64 architecture!

 Many variations in the chips, main ideas in the textbook!

Operating System Concepts – 9th Edition! 7.22! Silberschatz, Galvin and Gagne ©2013!
Example: The Intel IA-32 Architecture
 Supports both segmentation and segmentation with paging!
 Each segment can be 4 GB!
 Up to 16 K segments per process!
 Divided into two partitions!
 First partition of up to 8 K segments are private to process (kept in local descriptor table
(LDT))!
 Second partition of up to 8K segments shared among all processes (kept in global descriptor
table (GDT))!

 CPU generates logical address!


 Selector given to segmentation unit!
 Which produces linear addresses !

 Linear address given to paging unit!


 Which generates physical address in main memory!
 Paging units form equivalent of MMU!
 Pages sizes can be 4 KB or 4 MB!

Operating System Concepts – 9th Edition! 7.23! Silberschatz, Galvin and Gagne ©2013!
Logical to Physical Address Translation in IA-32

Operating System Concepts – 9th Edition! 7.24! Silberschatz, Galvin and Gagne ©2013!
Intel IA-32 Segmentation

Operating System Concepts – 9th Edition! 7.25! Silberschatz, Galvin and Gagne ©2013!
Intel IA-32 Paging Architecture

Operating System Concepts – 9th Edition! 7.26! Silberschatz, Galvin and Gagne ©2013!
Quizzes
 Binding of instructions and data to memory addresses is done at load time!
 Segmentation and paging are two alternative ways to manage the main memory!
 Instructions to be executed need to be in the main memory!
 The size of a program is limited by the size of the physical memory!
 A process cannot be executed if its logical address space is bigger than the physical address
space!
 An inverted page table contains, in each entry, a page number and a frame number!
 A hashed page table contains, in each entry, a page number and a frame number!
 Paging permits pages to be of arbitrary size!
 Segmentation permits segments to be of arbitrary size!
 A segment table length register (STLR) can be used to protect memory from illegal accesses!
 A page table length register (PTLR) can be used to protect memory from illegal accesses!
 A page table base register (PTBR) can be used to protect memory from illegal accesses!
 Swapping moves out of the main memory the entire image of a process, in order to make space
for the image of another process!
 External fragmentation implies internal fragmentation!
 Internal fragmentation implies external fragmentation!
 An advantage of paging is the possibility of sharing common code or data.!

7.27! Torroni, Real-Time Operating Systems M ©2013!


Exercise
 Consider a paging system with the page table stored in memory.!
 If a memory reference takes 200ns, how long does a paged memory
refrence take?!
 If we add TLBs, and 75% of all page-table references are found in the
TLBs, what is the effective memory reference time? (assume 0 time to
find a TLB page entry, in case of TLB hit)!

7.28! Torroni, Real-Time Operating Systems M ©2013!


Exercise
 Consider a logical address space of 64 pages of 1K words each, mapped
onto a physical memory of 32 frames.!
 How many bits are there in the logical address?!
 How many bits are there in the physical address?!

7.29! Torroni, Real-Time Operating Systems M ©2013!


Chapter 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!

Operating System Concepts – 9th Edition! 7.30! Silberschatz, Galvin and Gagne ©2013!
Objectives
 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!

Operating System Concepts – 9th Edition! 7.31! Silberschatz, Galvin and Gagne ©2013!
Background
 Code needs to be in memory to execute, but entire program rarely
used!
 Error code, unusual routines, large data structures!
 Entire program code not needed at same time!
 Consider ability to execute partially-loaded program!
 Program no longer constrained by limits of physical memory!
 Program and programs could be larger than physical memory!

 Virtual memory – separation of user logical memory from physical


memory!

Operating System Concepts – 9th Edition! 7.32! Silberschatz, Galvin and Gagne ©2013!
Virtual Memory That is
Larger Than Physical Memory

 Only part of the program needs to be in memory for execution !

Operating System Concepts – 9th Edition! 7.33! Silberschatz, Galvin and Gagne ©2013!
Virtual Address Space
 Logical address space can be much larger than physical address space !
 Easier to program!
 Enables sparse address spaces with holes left for growth, dynamically
linked libraries, etc!

Operating System Concepts – 9th Edition! 7.34! Silberschatz, Galvin and Gagne ©2013!
Virtual Address Space
 System libraries shared via mapping into virtual address space!

 Shared memory by mapping pages read-write into virtual address space!


 Pages can be shared during fork(), speeding process creation!

Operating System Concepts – 9th Edition! 7.35! Silberschatz, Galvin and Gagne ©2013!
Virtual Memory
 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!
 Easier to program!
 Allows address spaces to be shared by several processes!
 Allows for more efficient process creation!
 More programs running concurrently!
 More CPU utilization!
 Less I/O needed to load or swap processes
!
 Implementation of virtual memory by demand paging !

Operating System Concepts – 9th Edition! 7.36! Silberschatz, Galvin and Gagne ©2013!
Demand Paging
 Could bring entire process into memory at load time!
 Or bring a page into memory only when it is needed!
Less I/O needed, no unnecessary I/O!

 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!
 [“Swapper” moves entire process image]!

Operating System Concepts – 9th Edition! 7.37! Silberschatz, Galvin and Gagne ©2013!
Transfer of a Paged Memory to
Contiguous Disk Space

Operating System Concepts – 9th Edition! 7.38! Silberschatz, Galvin and Gagne ©2013!
Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(v ⇒ in-memory – memory resident, i ⇒ not-in-memory)!
 Initially valid–invalid bit is set to i on all entries!
 Example of a page table snapshot:
Frame #! valid-invalid bit!
v!
v!
v!
v!
i!
….!

i!
i!
!
page table!

 During address translation, if valid–invalid bit in page table entry!


is I ⇒ page fault!

Operating System Concepts – 9th Edition! 7.39! Silberschatz, Galvin and Gagne ©2013!
Page Table When Some Pages
Are Not in Main Memory

Operating System Concepts – 9th Edition! 7.40! Silberschatz, Galvin and Gagne ©2013!
Page Fault

 If there is a reference to a page that is not in memory, first reference to


that page will trap to operating system:!
page fault!

1. Operating system looks at internal table (in PCB) to check if legal


reference:!
Invalid reference ⇒ abort!

 Just not in memory ⇒ page in…!
2. Get empty frame!
3. Swap page into frame via scheduled disk operation!
4. Reset tables to indicate page now in memory
Set validation bit = v!
5. Restart the instruction that caused the page fault!

Operating System Concepts – 9th Edition! 7.41! Silberschatz, Galvin and Gagne ©2013!
Steps in Handling a Page Fault

Operating System Concepts – 9th Edition! 7.42! Silberschatz, Galvin and Gagne ©2013!
Aspects of Demand Paging
 Extreme case – start process with no pages in memory!
 OS sets instruction pointer to first instruction of process, non-memory-
resident -> page fault!
 And for every other process pages on first access!
 Pure demand paging!
 Actually, a given instruction could access multiple pages -> multiple page
faults!
 Pain decreased because of locality of reference!
 Hardware support needed for demand paging!
 Page table with valid / invalid bit!
 Secondary memory (swap device with swap space)!
 Instruction restart!

Operating System Concepts – 9th Edition! 7.43! Silberschatz, Galvin and Gagne ©2013!
Instruction Restart
 Consider an instruction that could access several different locations!
 Block move

!
!
!
!
 Restart the whole operation?!
 What if source and destination overlap?!
 What if block straddles page boundary?!
 either use buffer to keep old values!

 or force page fault at beginning of operation!

 Architecture/microcode must support proper restart (by being


aware of page faults)!

Operating System Concepts – 9th Edition! 7.44! Silberschatz, Galvin and Gagne ©2013!
Performance of Demand Paging
 Stages in Demand Paging!
1. Trap to the operating system!
2. Save the user registers and process state!
3. Determine that the interrupt was a page fault!
4. Check that the page reference was legal and determine the location of the page on the disk!
5. Issue a read from the disk to a free frame:!
1. Wait in a queue for this device until the read request is serviced!
2. Wait for the device seek and/or latency time!
3. Begin the transfer of the page to a free frame!
6. While waiting, allocate the CPU to some other user (if CPU scheduling)!
7. Receive an interrupt from the disk I/O subsystem (I/O completed)!
8. Save the registers and process state for the other user!
9. Determine that the interrupt was from the disk!
10. Correct the page table and other tables to show page is now in memory!
11. Wait for the CPU to be allocated to this process again!
12. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction!

Operating System Concepts – 9th Edition! 7.45! Silberschatz, Galvin and Gagne ©2013!
Performance of Demand Paging (Cont.)
 Page Fault Rate 0 ≤ p ≤ 1!
 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!
)!

Operating System Concepts – 9th Edition! 7.46! Silberschatz, Galvin and Gagne ©2013!
Demand 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 causes a page fault, then!
EAT = 8.2 microseconds. !
This is a slowdown by a factor of 40!!!
 If want performance degradation < 10 percent!
 220 > 200 + 7,999,800 x p
20 > 7,999,800 x p!
 p < .0000025!
 < one page fault in every 400,000 memory accesses!
!!
Operating System Concepts – 9th Edition! 7.47! Silberschatz, Galvin and Gagne ©2013!
Demand Paging Optimizations
 Copy entire process image to swap space at process load time!
 Then page in and out of swap space!
 Used in older BSD Unix!

 Demand page in from program binary on disk, but discard rather than paging out
when freeing frame!
 Used in Solaris and current BSD!

 Prepage at startup all/some pages a process will need, before they are referenced!
 Reduces large number of page faults that occurs at process startup!
 But if prepaged pages are unused, I/O and memory was wasted!
 Assume s pages are prepaged and α of the pages is used!
 Is cost of s * α save pages faults > or < than the cost of prepaging
s * (1- α) unnecessary pages? !
 α near zero ⇒ prepaging loses !

Operating System Concepts – 9th Edition! 7.48! Silberschatz, Galvin and Gagne ©2013!
Copy-on-Write
 Copy-on-Write (COW) allows both parent and child processes to
initially share the same pages in memory!
 If either process modifies a shared page, only then is the page
copied!
 COW allows more efficient process creation as only modified pages
are copied!
 In general, free pages are allocated from a pool of zero-fill-on-
demand pages!
 Why zero-out a page before allocating it?!
 vfork() variation on fork() system call has parent suspend and
child using address space of parent!
 Designed to have child call exec()
 Very efficient (page table is not copied)!

Operating System Concepts – 9th Edition! 7.49! Silberschatz, Galvin and Gagne ©2013!
After fork()

Operating System Concepts – 9th Edition! 7.50! Silberschatz, Galvin and Gagne ©2013!
After Page C Modified

Operating System Concepts – 9th Edition! 7.51! Silberschatz, Galvin and Gagne ©2013!
What Happens if There is no Free Frame?

 Used up by process pages!


 Also in demand from the kernel, I/O buffers, etc!
 How much to allocate to each?!

 Page replacement – find some page in memory, but not really in use,
page it out!
 Algorithm – terminate? swap out? replace the page?!
 Performance – want an algorithm which will result in minimum
number of page faults!

 Same page may be brought into memory several times!

Operating System Concepts – 9th Edition! 7.52! Silberschatz, Galvin and Gagne ©2013!
Page 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!

Operating System Concepts – 9th Edition! 7.53! Silberschatz, Galvin and Gagne ©2013!
Need For Page Replacement

Operating System Concepts – 9th Edition! 7.54! Silberschatz, Galvin and Gagne ©2013!
Basic Page Replacement

1. Find the location of the desired page on disk


!
2. 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!
 Write victim frame to disk if dirty
!
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
!
4. Continue the process by restarting the instruction that caused the trap!

Note now potentially 2 page transfers for page fault – increasing EAT!

Operating System Concepts – 9th Edition! 7.55! Silberschatz, Galvin and Gagne ©2013!
Page Replacement

Operating System Concepts – 9th Edition! 7.56! Silberschatz, Galvin and Gagne ©2013!
Page and Frame Replacement Algorithms

 Frame-allocation algorithm determines !


 How many frames to give each process!
 Which frames to replace!
 Page-replacement algorithm!
 Want lowest page-fault rate on both first access and re-access!
!
 Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page faults
on that string!
 String is just page numbers, not full addresses!
 Repeated access to the same page does not cause a page fault!
 In all our examples, memory has 3 frames and the reference string is !
! 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!

Operating System Concepts – 9th Edition! 7.57! Silberschatz, Galvin and Gagne ©2013!
Graph of Page Faults Versus
The Number of Frames

Operating System Concepts – 9th Edition! 7.58! Silberschatz, Galvin and Gagne ©2013!
First-In-First-Out (FIFO) Algorithm
 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!
 3 frames (3 pages can be in memory at a time per process)!

Operating System Concepts – 9th Edition! 7.59! Silberschatz, Galvin and Gagne ©2013!
First-In-First-Out (FIFO) Algorithm
 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!
 3 frames (3 pages can be in memory at a time per process)!
!
!

!
!
!

!
!
!

 15 page faults!
 How to track ages of pages? !
 Just use a FIFO queue!

Operating System Concepts – 9th Edition! 7.60! Silberschatz, Galvin and Gagne ©2013!
Exercise
 Reference string: 1,2,3,4,1,2,5,1,2,3,4,5!
 Evaluate FIFO replacement with 3 frames!
 Evaluate FIFO replacement with 4 frames!

7.61! Torroni, Real-Time Operating Systems M ©2013!


Belady’s Anomaly
 Adding more frames can cause more page faults!!

Operating System Concepts – 9th Edition! 7.62! Silberschatz, Galvin and Gagne ©2013!
Optimal Page Replacement Algorithm
 Replace page that will not be used for longest period of time!

 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!

Operating System Concepts – 9th Edition! 7.63! Silberschatz, Galvin and Gagne ©2013!
Optimal Page Replacement Algorithm
 Replace page that will not be used for longest period of time!

 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!

Operating System Concepts – 9th Edition! 7.64! Silberschatz, Galvin and Gagne ©2013!
Optimal Page Replacement Algorithm
 Replace page that will not be used for longest period of time!
 9 is optimal in our example!
!!
 How do you know this?!
 Can’t read the future!

 Used for measuring how well your algorithm performs!


 Lower bound!

Operating System Concepts – 9th Edition! 7.65! Silberschatz, Galvin and Gagne ©2013!
Least Recently Used (LRU) Algorithm
 Use past knowledge rather than future!
 Replace page that has not been used in the most amount of time!
 Associate time of last use with each page!

 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1!


!

Operating System Concepts – 9th Edition! 7.66! Silberschatz, Galvin and Gagne ©2013!
Least Recently Used (LRU) Algorithm
 Use past knowledge rather than future!
 Replace page that has not been used in the most amount of time!
 Associate time of last use with each page!

 12 faults – better than FIFO but worse than OPT!


 Generally good algorithm and frequently used!
 But how to implement?!
!

Operating System Concepts – 9th Edition! 7.67! Silberschatz, Galvin and Gagne ©2013!
LRU Algorithm (Cont.)
 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 find
smallest value!
 Search through table needed!
 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!
 But each update more expensive!
 LRU and OPT are cases of stack algorithms that don’t have
Belady’s Anomaly!
Operating System Concepts – 9th Edition! 7.68! Silberschatz, Galvin and Gagne ©2013!
Use Of A Stack to Record The
Most Recent Page References

Operating System Concepts – 9th Edition! 7.69! Silberschatz, Galvin and Gagne ©2013!
LRU Approximation Algorithms
 LRU needs special hardware and still slow!
 Reference bit!
 With each page associate a bit, initially = 0!
 When page is referenced bit set to 1!
 Replace any with reference bit = 0 (if one exists)!
 We do not know the order, however!
 Second-chance algorithm!
 Generally FIFO, plus hardware-provided reference bit!
 Clock replacement!
 If page to be replaced has !
 Reference bit = 0 -> replace it!
 reference bit = 1 then:!
– set reference bit 0, leave page in memory!
– replace next page, subject to same rules!

Operating System Concepts – 9th Edition! 7.70! Silberschatz, Galvin and Gagne ©2013!
Second-Chance (clock) Page-Replacement Algorithm

Operating System Concepts – 9th Edition! 7.71! Silberschatz, Galvin and Gagne ©2013!
Counting Algorithms

 Keep a counter of the number of references that have been made to


each page!
 Not common
!
 LFU Algorithm: replaces page with smallest count
!
 MFU Algorithm: based on the argument that the page with the
smallest count was probably just brought in and has yet to be used!

Operating System Concepts – 9th Edition! 7.72! Silberschatz, Galvin and Gagne ©2013!
Page-Buffering Algorithms
 Keep a pool of free frames, always!
 Then frame available when needed, not found at fault time!
 Read page into free frame and select victim to evict and add to free pool!
 When convenient, evict victim!
 Possibly, keep list of modified pages!
 When backing store otherwise idle, write pages there and set to non-
dirty!
 Possibly, keep free frame contents intact and note what is in them!
 If referenced again before reused, no need to load contents again from
disk!
 Generally useful to reduce penalty if wrong victim frame selected !

Operating System Concepts – 9th Edition! 7.73! Silberschatz, Galvin and Gagne ©2013!
Allocation of Frames

 Each process needs minimum number of frames!


 Example: IBM 370 – 6 pages to handle SS MOVE instruction:!
 instruction is 6 bytes, might span 2 pages!
 2 pages to handle from!
 2 pages to handle to!
 Maximum of course is total frames in the system!
 Two major allocation schemes!
 fixed allocation!
 priority allocation!
 Many variations!

Operating System Concepts – 9th Edition! 7.74! Silberschatz, Galvin and Gagne ©2013!
Fixed Allocation

 Equal allocation – For example, if there are 100 frames (after


allocating frames for the OS) and 5 processes, give each process 20
frames!
 Keep some as free frame buffer pool!

 Proportional allocation – Allocate according to the size of process!


 Dynamic as degree of multiprogramming, process sizes change!
si = size of process pi m = 64
S = ∑ si s1 = 10
m = total number of frames s2 = 127
s 10
ai = allocation for pi = i × m a1 = × 64 ≈ 5
S 137
127
a2 = × 64 ≈ 59
137

Silberschatz, Galvin and Gagne ©2013!


Operating System Concepts – 9th Edition! 7.75! €
Priority Allocation

 Use a proportional allocation scheme using priorities rather than size


!
 If process Pi generates a page fault,!
 select for replacement one of its frames!
 select for replacement a frame from a process with lower priority
number!

Operating System Concepts – 9th Edition! 7.76! Silberschatz, Galvin and Gagne ©2013!
Global vs. Local Allocation

 Global replacement – process selects a replacement frame from the


set of all frames; one process can take a frame from another!
 But then process execution time can vary greatly!
 But greater throughput so more common!
!
 Local replacement – each process selects from only its own set of
allocated frames!
 More consistent per-process performance!
 But possibly underutilized memory!

Operating System Concepts – 9th Edition! 7.77! Silberschatz, Galvin and Gagne ©2013!
Thrashing

 If a process does not have “enough” pages, the page-fault rate is very
high!
 Page fault to get page!
 Replace existing frame!
 But quickly need replaced frame back!
 This leads to:!
 Low CPU utilization!
 Operating system thinking 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!

Operating System Concepts – 9th Edition! 7.78! Silberschatz, Galvin and Gagne ©2013!
Thrashing (Cont.)

Operating System Concepts – 9th Edition! 7.79! Silberschatz, Galvin and Gagne ©2013!
Demand 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 > total memory size!
 Limit effects by using local or priority page replacement!

Operating System Concepts – 9th Edition! 7.80! Silberschatz, Galvin and Gagne ©2013!
Locality In A Memory-Reference Pattern

Operating System Concepts – 9th Edition! 7.81! Silberschatz, Galvin and Gagne ©2013!
Working-Set Model
 Δ ≡ working-set window ≡ a fixed number of page references
Example: 10,000 instructions!

 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 !


 Approximation of locality!

 if D > m ⇒ Thrashing!

 Policy if D > m, then suspend or swap out one of the processes!

Operating System Concepts – 9th Edition! 7.82! Silberschatz, Galvin and Gagne ©2013!
Working-Set Model

Operating System Concepts – 9th Edition! 7.83! Silberschatz, Galvin and Gagne ©2013!
Keeping 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!

Operating System Concepts – 9th Edition! 7.84! Silberschatz, Galvin and Gagne ©2013!
Page-Fault Frequency

 More direct approach than WSS!


 Establish “acceptable” page-fault frequency rate and use local
replacement policy!
 If actual rate too low, process loses frame!
 If actual rate too high, process gains frame!

Operating System Concepts – 9th Edition! 7.85! Silberschatz, Galvin and Gagne ©2013!
Working Sets and Page Fault Rates

Operating System Concepts – 9th Edition! 7.86! Silberschatz, Galvin and Gagne ©2013!
Memory-Mapped Files
 Memory-mapped file I/O allows file I/O to be treated as routine memory
access by mapping a disk block to a page in memory!
 A file is initially read using demand paging!
 A page-sized portion of the file is read from the file system into a
physical page!
 Subsequent reads/writes to/from the file are treated as ordinary
memory accesses!
 Simplifies and speeds file access by driving file I/O through memory
rather than read() and write() system calls!
 Also allows several processes to map the same file allowing the pages
in memory to be shared!
 But when does written data make it to disk?!
 Periodically and / or at file close() time!
 For example, when the pager scans for dirty pages!

Operating System Concepts – 9th Edition! 7.87! Silberschatz, Galvin and Gagne ©2013!
Memory-Mapped Files

Operating System Concepts – 9th Edition! 7.88! Silberschatz, Galvin and Gagne ©2013!

You might also like