You are on page 1of 13

Chapter 10: Virtual Memory

 Background
 Demand Paging
 Process Creation
 Page Replacement
 Allocation of Frames
 Thrashing
 Operating System Examples

Operating System Concepts


Logical vs. Physical Address Space
revised
 The concept of a logical address space that is bound to a
separate physical address space is central to proper
memory management.
 Logical address – generated by the CPU; also referred to as
virtual address.
 Physical address – address seen by the memory unit.

 Logical and physical addresses are the same in compile-


time and load-time address-binding schemes; logical
(virtual) and physical addresses differ in execution-time
address-binding scheme.

Operating System Concepts


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 is a technique that allows the execution of processes that are
not completely in memory. One major advantage of this scheme is that
programs can be larger than physical memory.”

 Virtual memory also allows processes to share files easily and to implement
shared memory.

Operating System Concepts


Virtual Memory That is Larger Than Physical Memory

Operating System Concepts


Concept and need of Demand Paging

 how an executable program might be loaded from disk into memory.?


 One option is to load the entire program in physical memory at
program execution time.
 a problem with this approach, is that we may not initially need the entire
program in memory.
 Example: a program with menu to specify list of available options.
 Loading the entire program into memory results in loading the executable
code for all options, regardless of whether an option is ultimately selected
by the user or not.
 Another option is to initially load pages only as they are needed. This
technique is known as demand paging and is commonly used in virtual
memory systems.
 pages are only loaded when they are demanded during program execution
 pages that are never accessed are thus never loaded into physical
memory.

Operating System Concepts


Demand Paging conti…
 A demand-paging system is similar to a paging system with swapping
(Figure 9.4) where processes reside in secondary memory (usually a
disk).

Operating System Concepts 10.6 Silberschatz, Galvin and Gagne 2002


Demand Paging conti…
 Rather than swapping the entire process into memory, we load only
pages that are needed.

 we use a lazy swapper in demand paging .

 A lazy swapper never swaps a page into memory unless that page will
be needed.

 We can view a process as a sequence of pages, rather than as one


large contiguous address space.
 instead of using the term swapper , the pager is the correct technical
term.
 A swapper manipulates entire processes, whereas a pager is
concerned with the individual pages of a process.
 Thus the “pager”, rather than swapper, in connection with demand
paging.
Operating System Concepts
Basic Concept of Demand Paging..
 When a process is to be swapped in, the pager guesses which pages
will be used before the process is swapped out again.

 Instead of swapping in a whole process, the pager brings only those


necessary pages into memory.
 it avoids reading into memory pages that will not be used anyway.
 decreasing the swap time
 Decrease the amount of physical memory needed.

 we need some form of hardware support to distinguish between the


pages that are in memory and the pages that are on the disk(that are
out of memory).

 The valid-invalid bit scheme can be used for this purpose.

Operating System Concepts


Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
 when bit is "valid/" then the associated page is both legal and in memory.
 If the bit is set to "invalid," the page either is not valid (that is, not in the logical address
space of the process) or is valid but is currently on the disk. Frame No# valid/invalid bit

1
1 1=valid page
1 0=invalid page
.
.
.
 Initially valid–invalid but is set to 0 on all entries.
0 entry is 0  page fault.
 During address translation, if valid–invalid bit in page table
0

Operating System Concepts


Page Table When Some Pages Are Not in Main Memory

Operating System Concepts


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


Steps for handling page fault….
 1. We check an internal table (usually kept with the process control
block)for this process to determine whether the reference was a valid or an
invalid memory access.
 2. If the reference was invalid, we terminate the process. If it was valid, but
we have not yet brought in that page, we now page it in.
 3. We find a free frame (by taking one from the free-frame list, for
example).
 4. We schedule a disk operation to read the desired page into the newly
allocated frame.
 5. When the disk read is complete, we modify the internal table kept with
the process and the page table to indicate that the page is now in memory.
 6. We restart the instruction that was interrupted by the trap. The process
can now access the page as though it had always been in memory.

Operating System Concepts


Steps in Handling a Page Fault

Operating System Concepts

You might also like