0% found this document useful (0 votes)
39 views32 pages

3.2. Virtual Memory

The document discusses virtual memory and demand paging. Virtual memory allows processes to execute even if they are larger than physical memory by storing portions of processes on disk. When a process attempts to access a memory page not in RAM, a page fault occurs and the OS brings the required page into memory from disk. The document covers demand paging, page tables, page replacement algorithms like FIFO and LRU, and challenges in implementing LRU.

Uploaded by

Aman E.
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)
39 views32 pages

3.2. Virtual Memory

The document discusses virtual memory and demand paging. Virtual memory allows processes to execute even if they are larger than physical memory by storing portions of processes on disk. When a process attempts to access a memory page not in RAM, a page fault occurs and the OS brings the required page into memory from disk. The document covers demand paging, page tables, page replacement algorithms like FIFO and LRU, and challenges in implementing LRU.

Uploaded by

Aman E.
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/ 32

3.

2 Virtual Memory

09/26/2023 virtual memory OS(CoSc2034) 1


Contents

– Background
– Demand Paging
– Advantages of VM: Process Creation
– Page Replacement
– Allocation of Frames
– Thrashing

09/26/2023 virtual memory OS(CoSc2034) 2


Background
o First requirement for execution is Instructions must be in physical
memory
o One Approach is Place entire logical address in main memory.
o The size of the program is limited to size of main memory.
o Normally entire program may not be needed in main memory.
 Programs have error conditions.
 Arrays, lists, and tables may be declared by 100 by 100
elements, but seldom larger than 10 by 10 elements.
 Assembler program may have room for 3000 symbols, although
average program may contain less than 200 symbols.
 Certain portions or features of the program are used rarely.
o Benefits of the ability to execute program that is partially in
memory:
 User can write programs and software for entirely large
virtual address space.
 More programs can run at the same time.
 Less I/O would be needed to load or swap each user program
into memory.
09/26/2023 virtual memory OS(CoSc2034) 3
Background(con’t…)
 Virtual memory is a technique that allows the execution of
processes that may not be completely in memory.
– Programs are larger than main memory.
– VM abstract main memory into an extremely large,
uniform array of storage.
 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.
– Frees the programmer from memory constraints.
 Virtual memory can be implemented via:
– Demand paging
– Demand segmentation
09/26/2023 virtual memory OS(CoSc2034) 4
Virtual Memory That is Larger Than Physical Memory

09/26/2023 virtual memory OS(CoSc2034) 5


Demand Paging
o Paging system with swapping.
– When we execute a process we swap into memory.
o For demand paging, we use lazy swapper.
– Never swaps a page into memory unless required.
– Bring a page into memory only when it is needed.
• Less I/O needed
• Less memory needed
• Faster response
• More users
o Page is needed  reference to it
– invalid reference  abort
– not-in-memory  bring to memory

09/26/2023 virtual memory OS(CoSc2034) 6


Presence Bit
 With each page table entry a presence bit is associated
(1  in-memory, 0  not-in-memory)
 Initially presence bit is set to 0 on all entries.
 Example of a page table snapshot.
Frame # presence bit
1
1
1

0
1

0
0
page table
 During address translation, if presence bit in page table
entry is 0  page fault.

09/26/2023 virtual memory OS(CoSc2034) 7


Page Table When Some Pages Are Not in Main
Memory

09/26/2023 virtual memory OS(CoSc2034) 8


Page Fault
o If there is ever a reference to a page, first reference
will trap to OS  page fault
o 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:

09/26/2023 virtual memory OS(CoSc2034) 9


Steps in Handling a Page Fault
1. Check the internal table,
to determine whether
this reference is valid or
invalid.
2. If the reference is
invalid, then trap to OS.
3. Find free frame.
4. Schedule disk operation.
5. Modify the internal table
to indicate that page is in
main memory.
6. Restart the instruction.

09/26/2023 virtual memory OS(CoSc2034) 10


Page Replacement
o Prevent over-allocation of memory by modifying page-
fault service routine to include page replacement.
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. And change
page table accordingly.
3. Read the desired page into the (newly) free frame.
Update the page table.
4. Restart the process.

09/26/2023 virtual memory OS(CoSc2034) 11


Page Replacement

09/26/2023 virtual memory OS(CoSc2034) 12


Page Replacement algorithm
o Page fault forces a choice
 No room for new page (steady state)
 Which page must be removed to make room for an
incoming page?
o How is a page removed from physical memory?
 If the page is unmodified, simply overwrite it: a copy
already exists on disk
 If the page has been modified, it must be written
back to disk: prefer unmodified pages?
o Better not to choose an often used page
 It’ll probably need to be brought back in soon

09/26/2023 virtual memory OS(CoSc2034) 13


Page Replacement Algorithms(con’t…)
o Produce lowest page-fault rate.
o Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page faults
on that string.
o In all our examples, the reference string is :1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4,
5.

Graph of Page
Faults Versus
The Number of
Frames

09/26/2023 virtual memory OS(CoSc2034) 14


Optimal page replacement algorithm
o replace the page that will be used furthest in the future
o Only works if we know the whole sequence!
o Nice, but not achievable in real systems!
Example: 4 frames and 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
string references
1
4 2
6 page faults
3

5
o Used for measuring how well your algorithm performs.
o How can you know what the future references will be?

09/26/2023 virtual memory OS(CoSc2034) 15


First-In-First-Out (FIFO) Algorithm
o Oldest page is replaced; FIFO queue; replace the head of the
queue
Example-1 :Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
mes

1 1 4 5 1 1 5 4
es2
3 fra

2 2 1 3 9 page faults m 2 1 5 10 page faults


f ra
3 3 g 4 3 3 2
g

2 4 i n
Usin

Us
o more frames  more page faults 4 4 3
o Another example FIFO page replacement algorithm.

09/26/2023 virtual memory OS(CoSc2034) 16


Least Recently Used (LRU) Algorithm
o The page that has not been used for longest period of time is
replaced.
o Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 5
2
3 5 4
4 3
LRU Page Replacement

o The performance is good. But, How to Implement ?


09/26/2023 virtual memory OS(CoSc2034) 17
LRU Algorithm Implementation
o Counter implementation
 keep counter in each page table entry
 Global counter increments with each CPU cycle
 Copy global counter to PTE counter on a reference to
the page
 For replacement, evict page with lowest counter value
o Stack implementation
 keep a stack of page numbers in a double link form:
• Page referenced:
– move it to the top
– requires 6 pointers to be changed
– Update is expensive
 No search for replacement
 Top is the most recently used page and bottom is the
LRU page.
09/26/2023 virtual memory OS(CoSc2034) 18
Use Of A Stack to Record The Most Recent Page
References

Performance issue: Stack and Counters


 The updating of stack or counter must be done on every memory reference.
 If we use interrupt for every reference, to allow software to update data
structures, it would slow every reference by a factor of 10.
 Few systems tolerate such degradation in performance.
 Sol:
– Systems follow LRU approximation implemented through hardware.

09/26/2023 virtual memory OS(CoSc2034) 19


LRU Approximation Algorithms
Second chance page replacement algorism
o Modify FIFO to avoid throwing out heavily used pages
o If reference bit is 0, throw the page out
 If reference bit is 1
 Reset the reference bit to 0
 Move page to the tail of the list
 Continue search for a free page
o Still easy to implement, and better than plain FIFO

referenced
unreferenced

09/26/2023 virtual memory OS(CoSc2034) 20


LRU Approximation Algorithms
Second-Chance (clock) Page-Replacement Algorithm
o Same functionality as second chance
o Simpler implementation
 “Clock” hand points to next page to replace
 If R=0, replace page
 If R=1, set R=0 and advance the clock hand
o Continue until page with R=0 is found
 This may involve going all the way around the clock…

09/26/2023 virtual memory OS(CoSc2034) 21


LRU Approximation Algorithms
Not frequently used(NRU)
o Each page has reference bit and dirty bit
 Bits are set when page is referenced and/or modified
o Pages are classified into four classes
 0: not referenced, not dirty
 1: not referenced, dirty
 2: referenced, not dirty
 3: referenced, dirty
o Clear reference bit for all pages periodically
 Can’t clear dirty bit: needed to indicate which pages need to be flushed to disk
 Class 1 contains dirty pages where reverence bit has been cleared
o Algorithm: remove a page from the lowest non-empty class
 Select a page at random from that class
o Easy to understand and implement
o Performance adequate (though not optimal)
Other algorithms: Counting Algorithms
o 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.
o MFU and LFU are not used
– Implementation is expensive
– Do not approximate OPT well
09/26/2023 virtual memory OS(CoSc2034) 22
Allocation of Frames
o Each process needs minimum number of pages allocation.
o If there is a single process, entire available memory can be
allocated.
o Multi-programming puts two or more processes in memory at
same time.
o We must allocate minimum number of frames to each
process.
o Two major allocation schemes.
– fixed allocation
– priority allocation

09/26/2023 virtual memory OS(CoSc2034) 23


Allocation of Frames(con’t…)
1. fixed partition
o Equal allocation – e.g., if 100 frames and 5 processes, give
each 20 pages.
o Proportional allocation – Allocate according to the size of
process.

2. Priority Allocation:- Use a proportional allocation


scheme using priorities rather than size.
o 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.

09/26/2023 virtual memory OS(CoSc2034) 24


Global vs. Local Allocation
o Global replacement – process selects a replacement
frame from the set of all frames; one process can take a
frame from another.
o Local replacement – each process selects from only its
own set of allocated frames.
o With local replacement # of frames does not change.
– Performance depends on the paging behavior of the
process.
– Free frames may not be used
o With global replacement, a process can take a frame from
another process.
– Performance depends not only paging behavior of that
process, but also paging behavior of other processes.
o In practice global replacement is used.

09/26/2023 virtual memory OS(CoSc2034) 25


Thrashing
o 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 is added to the system.
o Thrashing is High paging activity.
o Thrashing  a process is spending more time in
swapping pages in and out.
o If the process does not have number of frames
equivalent to number of active pages, it will very
quickly page fault.
o Since all the pages are in active use it will page fault
again.
09/26/2023 virtual memory OS(CoSc2034) 26
Thrashing

• Why does paging work?


Locality model
– Process migrates from one locality to another.
– Localities may overlap.
• Why does thrashing occur?
 size of locality > total memory size

09/26/2023 virtual memory OS(CoSc2034) 27


Causes of thrashing
 OS monitors CPU utilization
 If it is low, increases the degree of MPL
 Consider that a process enters new execution phase and
starts faulting.
 It takes pages from other processes
 Since other processes need those pages, they also fault,
taking pages from other processes.
 The queue increases for paging device and ready queue
empties
 CPU utilization decreases.
Solution: provide process as many frames as it needs.
 But how we know how many frames it needs ?
 Locality model provides hope.

09/26/2023 virtual memory OS(CoSc2034) 28


Locality model
 Locality is a set of pages that are actively used together.
 A program is composed of several different localities which may
overlap.
 Ex: even when a subroutine is called it defines a new locality.
 The locality model states that all the programs exhibit this memory
reference structure.
 This is the main reason for caching and virtual memory!
 If we allocate enough frames to a process to accommodate its
current locality, it fault till all pages are in that locality are in the MM.
Then it will not fault.
 If we allocate fewer frames than current locality, the process will
thrash.

09/26/2023 virtual memory OS(CoSc2034) 29


Working-Set Model
 Based on locality->Define a parameter ;
   working-set window  a fixed number of page references
Example: 10,000 instruction
 Most recent references are examined
 WSSi (working set of Process Pi) = total number of pages referenced in the most
recent  (varies in time)
 if  too small will not encompass entire locality.
 if  too large will encompass several localities.
 if  =   will encompass entire program.
 D =  WSSi  total demand frames
 if D > m  Thrashing
 Policy: if D > m, then suspend one of the processes.

09/26/2023 virtual memory OS(CoSc2034) 30


Working Set(con’t…)
 OS monitors the WS of each process allocates to that working set enough frames
equal to WS size.
 If there are enough extra frames, another process can be initiated.
 If D>m, OS suspends a process, and its frames are allocated to other processes.
 The WS strategy prevents thrashing by keeping MPL as high as possible.
 However, we have to keep track of working set.
Keeping track of Working Set
 Approximate with interval timer + a reference bit, -> Example:  = 10,000
 Timer interrupts after every 5000 time units.
 Keep in memory 2 bits for each page.
 Whenever a timer interrupts copy and sets the values of all reference bits to
0.
 If one of the bits in memory = 1  page in working set.
 Why is this not completely accurate?
 We can not tell when the reference was occurred.
 Accuracy can be increased by increasing frequency of interrupts which also
increases the cost.
09/26/2023 virtual memory OS(CoSc2034) 31
The End

09/26/2023 virtual memory OS(CoSc2034) 32

You might also like