Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Objectives
To describe the benefits of a virtual memory system
To explain the concepts of demand paging, page-replacement algorithms,
9.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Background
Virtual memory is a technique that allows the execution of processes that are not
completely in memory. One major advantage of this scheme is that programs can
be larger than physical memory.
Code needs to be in memory to execute, but entire program rarely used
Entire program code not needed at same time
Consider ability to execute partially-loaded program
Program no longer constrained by limits of physical memory
Each program takes less memory while running -> more programs run at the
same time
Increased CPU utilization and throughput with no increase in response
time or turnaround time
Less I/O needed to load or swap programs into memory -> each user program
Background
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
More programs running concurrently
Virtual memory makes the task of programming much easier; because the
programmer no longer needs to worry about the amount of physical memory
available; she can concentrate instead on the problem to be programmed
.
Virtual memory can be implemented via:
Demand paging
9.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Virtual Memory That is
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
Similar to paging system with swapping
(diagram on
right)
Page is needed
reference to it
invalid reference
abort
not-in-memory
bring to memory
(page fault)
Lazy swapper – never swaps a page into memory
unless page will be needed (Pure demand paging)
9.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Basic Concepts
With swapping, pager guesses which pages will be used before the process is
swapped out again
Instead of swapping in a whole process, the pager brings in only those pages into
memory
How to determine that set of pages?
Need new MMU functionality to implement demand paging
If pages needed are already memory resident
No difference from non demand-paging
If page needed and not memory resident
Need to detect and load the page into memory from storage
Without changing program behavior
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:
During MMU address translation, if valid–invalid bit in page table entry
is i
page fault
v
v
v
v
i
i
i
….
Frame #
valid-invalid bit
9.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Page Fault
1.
We check an internal table (usually kept with the process control block) for this process to determine
whether the reference was a valid or invalid memory access.
2.
If the reference was invalid, we terminate the process. If it was valid, but we have not yet brought in
that page, we now page it in.
3.
We find a free frame (by taking one from the free frame)
4.
We schedule a disk operation to read the desired page into the newly allocated frame.
5.
When the disk read is complete, we modify the internal table kept with the process and the page
table to indicate that the page is now in memory.
9.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Aspects of Demand Paging
Extreme case – start process with no pages in memory
OS sets instruction pointer to first instruction of process, which is on a
non-memory-resident, the process immediately faults for the page.
After this page is brought into memory, the process continues to execute,
faulting as many necessary until every page that it needs is in memory.
At that point, it can execute with no more faults. This scheme is
Pure demand
9.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Performance of Demand Paging (Cont.)
Demand paging can significantly affect the performance of a computer system. For
this, Let’s compute the Effective access time for a demand paged memory.
Performance of Demand Paging (Cont.)
Let p be the probability of a page fault . We would expect p to be close to zero – that is, we
would expect to have only a few page faults.
1.
Page Fault Rate 0
p
1
1.
if p = 0 no page faults
2.
if p = 1, every reference is a fault
2.
Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault time
OR
page fault overhead
+ swap page out
9.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
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
(EAT is directly proportional to the page fault rate)
If one access out of 1,000 causes a page fault, then
EAT = 8.2 microseconds.
Need for Page Replacement
If a process of 10 pages actually use half of them, then demand paging saves the I/O necessary to load the 5
pages that are never used. We could also increase degree of multiprogramming by running twice as processes.
If we had 40 frames, instead of running 4 processes, we can run 8 processes.
If we are increasing degree of multiprogramming, we are
over- allocating
memory.
6 processes of size 10 pages. And 40 frames are there.
Further, consider that system memory is not used only for holding programs . Buffers for I/O also consume a
significant amount of memory.
When a page fault occurs, and if no free frame is available to bring new page then the option is to terminate the
process. But, Demand paging is used to improve CPU utilization so instead of terminating it the concept of page
9.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Basic Page Replacement
If no frame is free, we find one that is not currently being used and free it. We can free a frame by writing
its contents to swap space and changing the page table (and all other tables) to indicate that the page is
no longer in memory. We can now use the freed frame to hold the page for which the process faulted.
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
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
9.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Page Replacement Algorithms
FIFO (First In First Out)
OPTIMAL
9.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
First-In-First-Out (FIFO) Algorithm
This is the simplest page replacement algorithm.
Associates with each page the time when that page was brought into memory.
When a page is to be replaced, the oldest one is selected.
Not necessary to record time, we can create a FIFO queue to hold all the pages in
memory. Replace the page at the head of the queue. And bring a page through
tail of queue.
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)
First-In-First-Out (FIFO) Algorithm
This algorithm is easy to understand and program. However, its performance is not
always good.
On the one hand
, the page replaced may be an initialization module
that was used a long time ago and is no longer needed.
On the other hand
, it could
contain a heavily used variable that was initialized early and is in constant use.
Notice that, even if we select for replacement a page that is in active use,
everything still works correctly. After we replace an active page with a new one, a
fault occurs almost immediately to retrieve the active page. Some other page will
need to be replaced to bring the active page back into memory.
Thus, a bad replacement choice increases the page fault rate and slows process
9.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
FIFO Illustrating Belady’s Anomaly
Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Optimal Algorithm
Replace page that will not be used for longest period of time
Use of this page replacement algorithm guarantees the lowest possible page
fault rate for a fixed number of frames.
Difficult to implement, because it requires future knowledge of the reference
string.
9.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
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? May require substantial hardware assistance. Problem is to determine an order for
LRU Algorithm (Cont.)
Counter implementation
We associate with Each page table entry a time of used field and add to the
CPU a logical clock or counter.
The clock is incremented for every memory reference. Whenever a reference to
a page is made, the contents of the clock register re copied to the time of use
field in the page table entry for that page. In this way, we always have the
“time” of the last reference to each page.
9.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
LRU Algorithm (Cont.)
Stack implementation
Another approach to implement LRU replacement is to keep a stack of page
numbers.
Whenever a page is referenced, it is removed from the stack and put on the top.
In this way, the most recently page is always at the top of the stack and the least
9.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Thrashing
If the process does not have the number of frames it needs to support pages in active use, it
will quickly page fault. At this point, it must replace some page. However, since all the pages
are in active use, it must replace a page that will be needed again right away. Consequently, it
quickly faults again, and again, and again, replacing pages that it must bring back in
immediately.
When we don’t have “enough” pages, the page-fault rate is very high
This leads to:
Low CPU utilization
Operating system thinking that it needs to increase the degree of multiprogramming, but
actually it gets decreased.
9.31 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition
Thrashing
The OS monitors CPU utilization. If CPU utilization is low, we increase the degree of
multiprogramming by introducing a new process to the system.
A Global page replacement algorithm is used ; it replaces pages without regard to the
Thrashing
The CPU scheduler sees the decreasing CPU utilization and increases the
degree of multiprogramming.
As a result, The new process tries to get started by taking frames from
9.33 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9th Edition