0% found this document useful (0 votes)
29 views34 pages

L11 - Virtual Memory

explanation about virtual memory

Uploaded by

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

L11 - Virtual Memory

explanation about virtual memory

Uploaded by

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

Virtual Memory

Dr. Sukanta Ghosh


System and Architecture
Content to be discussed
 Background
 Demand Paging
 Page Replacement
 Allocation of Frames
 Thrashing
 Demand Segmentation
 Operating System Examples

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


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.
 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

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Virtual Memory That is Larger Than Physical Memory

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Shared Library Using Virtual Memory

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Demand 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

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Transfer of a Paged Memory to Contiguous Disk Space

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(1  in-memory, 0  not-in-memory)
 Initially valid–invalid but is set to 0 on all entries
 Example of a page table snapshot:

Frame # valid-invalid bit


1
1
1
1
0

0
0
page table

 During address translation, if valid–invalid bit in page table entry is 0 


page fault

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Page Table When Some Pages Are Not in Main Memory

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Page Fault
 If there is ever a reference to a page, first reference will trap to
OS  page fault
 OS looks at another table to decide:
 Invalid reference  abort.
 Just not in memory.
 Get empty frame.
 Swap page into frame.
 Reset tables, validation bit = 1.
 Restart instruction: Least Recently Used
 block move

 auto increment/decrement location

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Steps in Handling a Page Fault

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


What 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

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


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 Adapted by: Dr. Sukanta Ghosh ©2005


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

3. Read the desired page into the (newly) free frame. Update the
page and frame tables.

4. Restart the process

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Page Replacement

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Page 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
 If the page to be searched is found among the frames then,
this process is known as Page Hit.
 If the page to be searched is not found among the frames
then, this process is known as Page Fault.
 When Page Fault occurs the problem arises, then the Page
Replacement Algorithm comes into picture.

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


First-In-First-Out (FIFO) Algorithm
 The fixed number of frames is filled up using first frames present.
 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)

1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
 4 frames

1 1 5 4
2 2 1 5 10 page faults
3 3 2

4 4 3
 FIFO Replacement – Belady’s Anomaly
 more frames  more page faults

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


FIFO Page Replacement

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


To Practice
 Reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
 Frame Size: 3 and 4

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Optimal Algorithm
 Replace page that will not be used for longest period of time.
 If referred page is already present, increment hit count.
 If not present, find if a page that is never referenced in future. If such
a page exists, replace this page with new page. If no such page
exists, find a page that is referenced farthest in future. Replace this
page with new page.

 No. of hits = 11
 No. of misses = 9

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


For Practice
 Reference string 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 4, 0
 Frame Size: 3

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Least Recently Used (LRU) Algorithm
 Replace the page with the page which is less dimension of time
recently used page in the past.
 Reference string 7 0 1 2 0 3 0 4 2 3 0 3 2

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


LRU Page Replacement

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


For Practice
 Reference string: 6, 1, 1, 2, 0, 3, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
 Frame Size: 3

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Counting Algorithms
 Keep a counter of the number of references that have been
made to each page

 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 Adapted by: Dr. Sukanta Ghosh ©2005


Allocation of Frames

 If you have numerous processes, frame allocation techniques are


utilized to determine how many frames to provide to each process.

 Two major allocation schemes


 fixed allocation
 priority allocation

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Frame Allocation Constraints
 The Frames that can be allocated cannot be greater than total
number of frames.
 Each process should be given a set minimum amount of frames.
 When fewer frames are allocated then the page fault ration
increases and the process execution becomes less efficient
 There ought to be sufficient frames to accommodate all the many
pages that a single instruction may refer to

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Fixed Allocation

 Equal allocation – For example, if there are 100 frames and 5


processes, give each process 20 frames.
 We divide the number of frames by number of processes.
 We get the number of frames we must provide for each process.
 Proportional allocation – Allocate according to the size of process.
 Issues:
 Not Logical
 Unused frames will eventually be wasted

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


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 Adapted by: Dr. Sukanta Ghosh ©2005


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
 Local replacement – each process selects from only its
own set of allocated frames

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Thrashing

 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

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Thrashing (Cont.)

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Windows XP
 Uses demand paging with clustering. Clustering brings in pages
surrounding the faulting page.
 Processes are assigned working set minimum and working set
maximum
 Working set minimum is the minimum number of pages the process
is guaranteed to have in memory
 A process may be assigned as many pages up to its working set
maximum
 When the amount of free memory in the system falls below a
threshold, automatic working set trimming is performed to
restore the amount of free memory
 Working set trimming removes pages from processes that have
pages in excess of their working set minimum

Operating System Concepts Adapted by: Dr. Sukanta Ghosh ©2005


Thank You

You might also like