virtual memory operating system
TRANSCRIPT
Virtual Memory
Virtual Memory
Virtual memory is a technique that is used to execute processes that are not in main memory completely. The remaining part are placed on the backing storage.
Demand paging
It is a technique that is used to bring a page into memory only when it is needed.
Demand paging cycle Page is needed reference to it Referenced page not in real memory Page Fault Just not in memory Demand paging Find free frame Swap page into frame Reset tables to indicate page now in memory Restart the instruction that caused the page fault
Page Replacement
“Page replacement completes separation between logical memory and physical memory large virtual memory can be provided on a smaller physical memory”
How to solve page faults?
It terminates the program immediatelyOr
Swaps out program & frees all of its frames It reduces the degree of multiprogramming
Basic Page ReplacementSteps performed by OS while replacing a page upon a page fault:
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; if the victim page is modified, write it back to disk.
3. Bring the desired page into the (new) free frame; update the page and frame tables
4. Restart the process
Page-replacement algorithm
Want lowest page-fault rate on both first access and re-access
There is particular string of memory references. String is just page numbers, not full addresses Repeated access to the same page does not
cause a page fault Results depend on number of frames available
In all our examples, the reference string of referenced page numbers is 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
First In First Out (FIFO) Algorithm
Oldest page in main memory is the one which will be selected for replacement
Easy to implement, keep a list, replace pages from the tail and add new pages at the head.
First-In-First-Out (FIFO) Algorithm
Adding more frames can cause more page faults!
Belady’s Anomaly
15 page faults
FIFO Illustrating Belady’s Anomaly
Optimal Algorithm Replace page that will not be used for longest period of time How do you know this?
Used for measuring how well your algorithm performs
Least Recently Used (LRU) Algorithm Use past knowledge rather than future Replace page that has not been used in the most amount of time
12 faults – better than FIFO but worse than OPT Generally good algorithm and frequently used
LRU Approximation Algorithm
Reference bitWith each page associate a bit, initially= 0When page is referenced bit set to 1
Additional Reference Bits We can gain additional ordering information
by recording the reference bits at regular interval
We can keep 8-bit byte for each page in a table in memory
The OS shifts the reference bit for each page into the high-order bit of its 8-bit byte, shifting the other bit right by 1 bit and discard the low-order bit
Second Chance Algorithm
This Algorithm is a FIFO replacement algorithm wit consideration given to the reference bit .when a page has been selected for replacement , its reference bit is inspected .
When a page is given a second chance its reference bit is cleared and its arrival time reset to the current time
Enhanced Second-Chance Algorithm
Improve algorithm by using reference bit and modify bit (if available) in concert
1. (0, 0) neither recently used not modified – best page to replace
2. (0, 1) not recently used but modified – not quite as good, must write out before replacement
3. (1, 0) recently used but clean – probably will be used again soon
4. (1, 1) recently used and modified – probably will be used again soon and need to write out before replacement
Allocation of Frames
Each process needs minimum number of pages Various allocation approaches
› fixed allocation › global allocation› local allocation
Fixed Allocation Equal allocation – For example, if there are 100
frames and 5 processes, give each process 20 frames.
Proportional allocation – Allocate according to the size of process
mSspa
m
sS
ps
iii
i
ii
for allocation
frames ofnumber total
process of size
Example: 5964
137127
564137101271064
2
1
2
a
a
ssm
i
Global versus Local Allocation
When a page fault occurs for a process and we need page replacement, there are two general approaches: › Global replacement – select a victim frame
from the set of all frames; one process can take a frame from another
› Local replacement – select a victim frame only from the frames allocated to the process. A process uses always its allocated frames
Thrashing
At some time , the processor may be spend most of its time swapping pages and doing little productive work, this is known as Thrashing.
Figure:
Thrashing Cause
It is caused by under allocation of minimum no. of pages required by a process , forcing it to continuously page fault.
Working setIt is a collection of pages, which is actively referenced by a process . It should be kept in main memory to run efficiently.
Working sets change during process execution . pages are added or deleted and critical changes may occur.
Page Size:It is a hardware design issue . Small pages reduce the amount of internal fragmentation. Large pages reduce the size of the page table.
In general small pages have better locality than large pages.