0% found this document useful (0 votes)
26 views36 pages

Os Chapter 9

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

Os Chapter 9

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

Operating System

Chapter 9: Virtual Memory


Virtual Memory

Virtual Memory is a space where large programs can store


themselves in form of pages while their execution and only
the required pages or portions of processes are loaded into
the main memory.

This technique is useful as large virtual memory is provided


for user programs when a very small physical memory is
there.
VIRTUAL MEMORY

In real scenarios, most processes never need all their pages at once, for
following reasons :

 Error handling code is not needed unless that specific error occurs, some
of which are quite rare.

 Arrays are often over-sized for worst-case scenarios, and only a small
fraction of the arrays are actually used in practice.

 Certain features of certain programs are rarely used.


Benefits of having Virtual Memory
 Large programs can be written, as virtual space available is huge compared to physical memory.

 Less I/O required, leads to faster and easy swapping of processes.

 More physical memory available, as programs are stored on virtual memory, so they occupy very
less space on actual physical memory.

Implementation of Virtual Memory

● Demand Paging
● Demand Segmentation
What is Demand Paging?

 The basic idea behind demand paging is that when a process is swapped in, its
pages are not swapped in all at once.

 Rather they are swapped in only when the process needs them(On demand).

 This is termed as lazy swapper, although a pager is a more accurate term.

 Initially only those pages are loaded which will be required the process
immediately.
Demand Paging
PAGE TABLE

● v=>page in memory
● i=> page not in memory
● During MMU address
mapping
○ i=> page fault occurs
Example of Demand Paging
PAGE FAULT

 When we try to refer to a page which is not in memory then page fault occurs.

 Since actual physical memory is much smaller than virtual memory, page faults
happen.

 In case of page fault, Operating System might have to replace one of the existing
pages with the newly needed page.

 Different page replacement algorithms suggest different ways to decide which page
to replace. The target for all algorithms is to reduce the number of page faults.
Steps to handle the page fault

When the process requires any of the page that is not loaded into the memory, a page fault
trap is triggered and following steps are followed,

1. The memory address which is requested by the process is first checked, to verify the request made by
the process is valid or invalid.
2. If its found to be invalid, the process is terminated.
3. In case the request by the process is valid, a free frame is located, possibly from a free-frame list,
where the required page will be moved.
4. A new operation is scheduled to move the necessary page from disk to the specified memory location. (
This will usually block the process on an I/O wait, allowing some other process to use the CPU in the
meantime. )
5. When the I/O operation is complete, the process's page table is updated with the new frame number,
and the invalid bit is changed to valid.
6. The instruction that caused the page fault must now be restarted from the beginning.
Steps to handle the page fault
Performance of Demand Paging

● Page fault rate: 0 ≤ p ≤ 1


○ If p=0, no page fault
○ If p=1, every reference is a fault

Effective Access Time(EAT)=(1-p)*memory access + p(page


fault overhead + swap page out+ swap page in)
Page Replacement

As studied in Demand Paging, only certain pages of a process are loaded initially into the
memory. This allows us to get more number of processes into the memory at the same time. but
what happens when a process requests for more pages and no free memory is available to bring
them in.
Following steps can be taken to deal with this problem :
 Put the process in the wait queue, until any other process finishes its execution thereby
freeing frames.

 Or, remove some other process completely from the memory to free frames.

 Or, find some pages that are not being used right now, move them to the disk to get free
frames. This technique is called Page replacement and is most commonly used. We have
some great algorithms to carry on page replacement efficiently.
Basic Page Replacement

 Find the location of the page requested by ongoing process on the


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 any existing
frame to be replaced, such frame is known as victim frame.
 Write the victim frame to disk.
 Change all related page tables to indicate that this page is no longer
in memory.
 Move the required page and store it in the frame.
 Adjust all related page and frame tables to indicate the change.
 Restart the process that was waiting for this page.
Basic Page Replacement
How to make a reference string?
 The string of memory references is called reference string. Reference
strings are generated artificially by recording the address of each memory
reference.
How to make a
 If we have a reference to a page p, then any immediately following

reference
references to page p will never cause a page fault.

string?
 Page p will be in memory after the first reference; the immediately following
references will not fault.

For example, consider the following sequence of addresses −


123,124,123,,215,213,215,610,625,1234,76,96,110,119
If page size is 100, then the reference string is 1,2,6,12,0,1
:
FIFO Page Replacement

 A very simple way of Page replacement is FIFO (First in First Out)

 As new pages are requested and are swapped in, they are added
to tail of a queue and the page which is at the head becomes the
victim.

 Its not an effective way of page replacement but can be used for
small systems.
FIFO
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.
FIFO Illustrating Belady’s Anomaly
Optimal Page Replacement
An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms. An
optimal page-replacement algorithm exists, and has been called OPT or MIN.
Replace the page that will not be used for the longest period of time in future. Use the time when
a page is to be used.
Optimal Page Replacement
Features of Optimal Page Replacement
Algorithm

 Optimal algorithm results in the fewest number of page faults. This algorithm is difficult to
implement.

 An optimal page-replacement algorithm method has the lowest page-fault rate of all algorithms.
This algorithm exists and which should be called MIN or OPT.

 Replace the page which unlike to use for a longer period of time. It only uses the time when a
page needs to be used.
Least Recently Used (LRU) algorithm

Page which has not been used for the longest time in past is the one which will be selected for replacement.
Least Recently Used (LRU) algorithm
LRU Implememtation

Counter implementation

 Each page-table entry has a counter; every time the page is referenced
through this entry, copy the current clock value into the counter

 When a page needs to be changed, look at the counters to find smallest


Value

 Search through the page-table needed; to find the LRU page


LRU Implememtation

Stack implementation

Keep a stack of page numbers in a double link form, with head and tail
pointers:

Whenever a page is referenced:


 move it to the top; most recently used page is always at the top of stack
 requires 6 pointers to be changed

But each update more expensive

No search for replacement; as LRU page is always at the bottom of the stack

NOTE: LRU and OPT are cases of stack algorithms that don’t have Belady’s
Anomaly
Advantages of Virtual Memory

Virtual memory helps to gain speed when only a particular segment of the program is
required for the execution of the program.

It is very helpful in implementing a multiprogramming environment and less I/O is required.

It allows you to run more applications at once.

It helps you to fit many large programs into smaller programs.

Common data or code may be shared between memory.

Process may become even larger than all of the physical memory.
Disadvantages of Virtual Memory

 Applications may run slower if the system is using virtual memory.

 Likely takes more time to switch between applications.

 It reduces system stability.

 It doesn’t offer the same performance as RAM.

 It negatively affects the overall performance of a system.


Q1. Assume that we have a demand-paged memory. The page table is held in
registers. It takes 8 milliseconds to service a page fault if an empty frame is
available or if the replaced page is not modified, and 20 milliseconds if the replaced
page is modified. Memory-access time is 100 nanoseconds.

Assume that the page to be replaced is modified 70 percent of the time. What is
the maximum acceptable page-fault rate for an effective access time of no more
than 200 nanoseconds?

Solution:
0.2 microsec = (1 - P) * 0.1 microsec + (0.3P) * 8 millisec + (0.7P) * 20 millisec

0.1 = -0.1P + 2400P + 14000P


0.1 ~= 16,400P
Q2. Consider a demand-paging system with a paging disk that has an average access
and transfer time of 20 milliseconds. Addresses are translated through a page table in
main memory, with an access time of 1 microsecond per memory access. Thus, each
memory reference through the page table takes two accesses. To improve this time, we
have added an associative memory that reduces access time to one memory reference, if
the page-table entry is in the associative memory.

Assume that 80 percent of the accesses are in the associative memory and that, of the
remaining, 10 percent (or 2 percent of the total) cause page faults. What is the effective
memory access time?

Solution: Effective access time =


(0.8)*(1 microsec) + (0.18)*(2microsec)+(0.02)*(20002microsec)
= 401.2 msec
= 0.4012 millisec
Questions based on Virtual Memory

Q1. What is difference between demand paging n pure demand paging?

In demand paging, a page is not loaded into main memory until it is needed. In pure demand paging,
even a single page is not loaded into memory initially. Hence pure demand paging causes a page fault.

Q2. Define lazy swapper.

Rather than swapping the entire process into main memory, a lazy swapper is used. A lazy swapper
never swaps a page into memory unless that page will be needed.

Q3. What is address binding?


The process of associating program instructions and data to physical memory addresses is called
address binding, or relocation.
Q4. Assume that you have a page-reference string for a process with m frames (initially
all empty). The page reference string has length p; n distinct page numbers occur in it.
Answer these questions for any page-replacement algorithms:
a. What is a lower bound on the number of page faults?
b. What is an upper bound on the number of page faults?

Solution:
a. n
b. P

Q5. A certain computer provides its users with a virtual-memory space of 2^32 bytes.
The computer has 2^18 bytes of physical memory. The virtual memory is implemented
by paging, and the page size if 4,096 bytes. A user process generates the virtual address
11123456. Explain how the system establishes the corresponding physical location.

The virtual address in binary is:


0001 0001 0001 0010 0011 0100 0101 0110
Since the page size is 2^12, the page table size is 2^20. Therefore the low-order 12 bits
"0100 0101 0110" are used as the displacement into the page, while the remaining 20 bits
"0001 0001 0001 0010 0011" are used as reference to page nos. in the page table.
Q9. Consider the following page-reference string:
1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6
How many page faults would occur for the following replacement algorithms, assuming
one, two, three, four, five, six, or seven frames? Remember that all frames are initially
empty, so your first unique pages will all cost one fault each.
* LRU replacement.
* FIFO replacement.
• Optimal replacement.

Solution:
Number of frames LRU FIFO Optimal
1 20 20 20
2 18 18 15
3 15 16 11
4 10 14 8
5 8 10 7
6 7 10 7
7 7 7 7
Q. Suppose there is a system with 128KB of memory with no memory initially allocated. Given the
following sequence of requests by the processes, show the memory layout at every stage for
worst-fit and Best fit allocation algorithm.

Process No. Nature Of Request Amount of Memory to be requested (in KB)


P0 Allocation 20
P1 Allocation 15
P2 Allocation 10
P3 Allocation 25
P0 Deallocation
P3 Deallocation
P4 Allocation 8
P5 Allocation 10
I. Consider a logical address space of 64 pages with 2-KB frame size mapped onto a
physical memory of 128 KB.
a. How many bits are there in the logical and physical addresses?
b. What is the breakup of offset and page number in the logical address?
c. Assuming demand paging environment with the list of free frames as 9, 0, 3, 7, 4 and the
page table (- implies the page is not in main memory):
Page No 0 1 2 3 4 5 6 7
Frame No 2 5 - 6 - 1 - -
Convert the following logical addresses (in binary) into the corresponding physical address (in
binary):
• 00001001101010111
• 00001101101101101
• 00010001110100100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy