Lecture 25

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Lecture 23:

Virtual Memory

Operating Systems(CS1108)
Dr. Anjali
Assistant Professor
JKL University
Virtual Memory

Virtual memory is a technique that allows the execution of processes


that are not completely in memory. The ability to execute a program
that is only partially in memory would confer many benefits:
• A program would no longer be constrained by the amount of physical
memory that is available. Users would be able to write programs for an
extremely large virtual address space, simplifying the programming
task.
• Because each user program could take less physical memory, more
programs could be run at the same time, with a corresponding increase
in CPU utilization and throughput but with no increase in response time
or turnaround time.
• Less I/O would be needed to load or swap user programs into memory, so
each user program would run faster.
Figure 1: Diagram showing virtual memory that is larger than physical memory
Demand Paging

• Demand Paging is a way to load pages only when they are


needed/required initially. This method is termed as demand paging
and is commonly utilized in virtual memory systems.
• A demand - paging scheme is similar to a paging system with
swapping feature where processes exist in secondary memory
(typically in a disk). As you want to execute any process, you swap it
into memory internally.
Transfer of a paged memory to contiguous
disk space
Hardware Requirement for Demand Paging

• Page table: Page table can mark an entry invalid or unacceptable


using a valid-invalid bit.
• Secondary memory: Secondary memory retains those pages which
are not there in main memory. The secondary memory is generally
a high-speed disk. It is also known as a swap device, and the
segment of disk used for this purpose is termed as swap space
Page Fault Handling
Page Fault Handling Steps

• if a page is needed that was not originally loaded up, then a page fault trap is generated, which must
be handled in a series of steps:
• The memory address requested is first checked, to make sure it was a valid memory request.
• If the reference was invalid, the process is terminated. Otherwise, the page must be paged in.
• A free frame is located, possibly from a free-frame list.
• A disk operation is scheduled to bring in the necessary page from disk. (This will usually block the
process on an I/O wait, allowing some other process to use the CPU in the meantime.)
• 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 indicate that this is now a valid page reference.
• The instruction that caused the page fault must now be restarted from the beginning, (as soon as this
process gets another turn on the CPU.)
• In an extreme case, NO pages are swapped in for a process until they are requested by page faults. This
is known as pure demand paging.
• In theory each instruction could generate multiple page faults. In practice this is very rare, due to
locality of reference.
Performance of Demand Paging

• Suppose that a normal memory access requires 200 nanoseconds, and


that servicing a page fault takes 8 milliseconds. (8,000,000 nanoseconds,
or 40,000 times a normal memory access.) With a page fault rate of p,
(on a scale from 0 to 1), the effective access time is now:

• EMAT= ( 1 - p ) * ( 200 ) + p * 8000000


• = 200 + 7,999,800 * p

• which clearly depends heavily on p! Even if only one access in 1000


causes a page fault, the effective access time drops from 200
nanoseconds to 8.2 microseconds, a slowdown of a factor of 40 times. In
order to keep the slowdown less than 10%, the page fault rate must be
less than 0.0000025, or one in 399,990 accesses
How to calculate Slow down?

• A: If there is no page fault then, the average memory access time


will be 200 ns
• B: If there is page fault in every 1000 instructions then the
memory access time becomes 8.2 microseconds
• Then the slow down in compared to case A is
. ∗

• Then we can say that the slow down is by a factor of 40.
Effective Memory Access Time

• Generally Effective memory access time can be defined as:


• Let Main memory access time is: m
• Page fault service time is: s
• Page fault rate is : p
• Then, Effective memory access time = (p*s) + (1-p)*m
Practice

• Example 1: Effective memory access time for a system is given


as 1 microseconds, and the average page fault service time is
given as 10 milliseconds. Let p=0.001.
• Then, effective access time is
• EMAT = 0.001 * 10 msec +(1 – 0.001) * 1 µsec
• = 0.001 * 10 000 µsec + 0.999 * 1 µsec
• 10 µsec + 1 µsec = 11 µsec
Page Replacement Algorithms

• FIFO
• Optimal
• LRU
FIFO

• Example1 . Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7,


2. the number of frames in the memory is 3. Find out the
number of page faults respective to: FIFO Page Replacement
Algorithm
Optimal

• Example 1: A system uses 3 page frames for storing process


pages in main memory. It uses the Optimal page replacement
policy. Assume that all the page frames are initially empty. What
is the total number of page faults that will occur while
processing the page reference string given below- 4 , 7, 6, 1,
7, 6, 1, 2, 7, 2
LRU

• A system uses 3 page frames for storing process pages in main


memory. It uses the Least Recently Used (LRU) page
replacement policy. Assume that all the page frames are initially
empty. What is the total number of page faults that will occur
while processing the page reference string given below-
• 4 , 7, 6, 1, 7, 6, 1, 2, 7, 2
Frame allocation algorithms

• Equal allocation: In a system with x frames and y processes, each


process gets equal number of frames, i.e. x/y.
• Proportional allocation: Frames are allocated to each process
according to the process size.
For a process pi of size si, the number of allocated frames is ai =
(si/S)*m, where S is the sum of the sizes of all the processes and m
is the number of frames in the system.
Example

• Say, there are 5 processes having 5, 10, 30, 40 and 50 frames


respectively. Total 60 Frame are available.
• Equal Allocation = 60 / 5 = 12 frames will be allocated each process.
• Proportional allocation :
• Mi=60
• Si= 5, S2=10, S3= 30, S4=40 and S5 =50
• A1= 5/135 x 60 = Approx 2
• A2 = 10/135 x60 = Approx 4
• A3 = 30/135 x 60 = Approx 13
• A4 = 40/135x60= Approx 18
• A5 = 50/135x60= Approx 22
Thrashing

• If a process does not have


“enough” pages, the page-
fault rate is very high.
• This leads to: low CPU
utilization due to [swap in]
and out [swap out] due to high
I/O [due to high page fault].

As the degree of multiprogramming increases , number of free frames as well number of pages allocated to each
process decreases, which gives rise to the increase in page fault rate and ultimately THRASHING.
Technique to handle Thrashing: Working Set
Model

• The working-set model is based on the assumption of locality.


• This model uses a parameter, ∆, to define the working-set window.
The idea is to examine the most recent ∆ page references. The set
of pages in the most recent ∆ page references is the working set.
• If a page is in active use, it will be in the working set. If it is no
longer being used, it will drop from the working set ∆ time units
after its last reference.
Example: if ∆= 10 memory references,
Example 2

• Let the page reference and the working set window be c c d b c e c e a


d and 4, respectively. The initial working set at time t=0 contains the
pages {a,d,e}, where a was referenced at time t=0, d was referenced at
time t=−1, and e was referenced at time t=−2. Determine the total
number of page faults and the average number of page frames used by
computing the working set at each reference.

You might also like