VIRTUAL MEMORY

BY,
SHASHANK SHETTY
WHAT IS VIRTUAL MEMORY???
 Entire process in memory before it can be executed.
 Virtual memory is a technique that allows execution
of the processes that are not completely in memory.

 Program can be larger than physical memory.
Virtual memory – separation of user logical memory
from physical memory.
 Memory storage limitation can be freed.
Virtual memory is not easy to implement.
BACKGROUND
Only part of the program needs to be in memory for
execution.
The need of instruction in physical memory to be
executed seems to be necessary & reasonable, but its
unfortunate.
The entire program is not needed.
 Executing the program partially in memory has
benefits:
 Program would no longer be constrained by the
amount of physical memory that is available.
 If programs could take less physical memory, more
programs could be run at the same time.
Virtual Memory That is Larger Than Physical Memory


 Thus, running a program that is not entirely
in memory would benefit both the system and the
user.
 Virtual memory can be implemented via:
• Demand paging
• Demand segmentation
Virtual-address Space
The virtual
address space of a
process refers to
the logical (or
virtual) view
of how a process
is stored in
memory.
Shared Library Using Virtual Memory
 In addition to separating logical memory from physical memory,
virtual memory also allows files and memory to be shared by
two or more processes through page sharing. This leads to the
following benefits:
System libraries can be shared by several processes through
mapping of the shared object into a virtual address space.
Virtual memory enables processes to share memory.

Virtual memory can allow pages to be shared during process
creation with the forkO system call, thus speeding up process
creation.
DEMAND PAGING
 Consider how an executable program might be
loaded from disk to memory.
 One option, load the entire program in physical
memory.
Ex: User selection program
 Second approach, load the pages only as they
needed ( DEMAND PAGING).
 A demand paging system is similar to the paging
system with swapping.
• 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
Transfer of a Paged Memory to Contiguous Disk Space
Valid-Invalid Bit
• With each page table entry a valid–invalid bit is associated
(1  in-memory, 0  not-in-memory)
• Initially valid–invalid bit 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
Page Table When Some Pages Are Not in Main
Memory
Page Fault
• What happens if the process tries to access a page and
that page was not brought into memory??????
• 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
Steps in Handling a Page Fault
 The hardware to support Demand paging:
 Page Table
 Secondary Memory
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
Performance of Demand Paging
• Page Fault Rate 0  p  1.0
– if p = 0 no page faults
– if p = 1, every reference is a fault

• Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead
+ [swap page out ]
+ swap page in
+ restart overhead)
Demand Paging Example
If we take an average page-fault service time of
8 milliseconds and a memory-access time of 200
nanoseconds, then the effective access time in
nanoseconds is
Copy-on-Write
• Copy-on-Write (COW) allows both parent and
child processes to initially share the same pages
in memory
If either process modifies a shared page, only
then is the page copied
• COW allows more efficient process creation as
only modified pages are copied

• Free pages are allocated from a pool of zeroedout pages
Virtual memory

Virtual memory

  • 1.
  • 2.
    WHAT IS VIRTUALMEMORY???  Entire process in memory before it can be executed.  Virtual memory is a technique that allows execution of the processes that are not completely in memory.  Program can be larger than physical memory. Virtual memory – separation of user logical memory from physical memory.  Memory storage limitation can be freed. Virtual memory is not easy to implement.
  • 3.
    BACKGROUND Only part ofthe program needs to be in memory for execution. The need of instruction in physical memory to be executed seems to be necessary & reasonable, but its unfortunate. The entire program is not needed.  Executing the program partially in memory has benefits:  Program would no longer be constrained by the amount of physical memory that is available.  If programs could take less physical memory, more programs could be run at the same time.
  • 4.
    Virtual Memory Thatis Larger Than Physical Memory 
  • 5.
     Thus, runninga program that is not entirely in memory would benefit both the system and the user.  Virtual memory can be implemented via: • Demand paging • Demand segmentation
  • 6.
    Virtual-address Space The virtual addressspace of a process refers to the logical (or virtual) view of how a process is stored in memory.
  • 7.
    Shared Library UsingVirtual Memory
  • 8.
     In additionto separating logical memory from physical memory, virtual memory also allows files and memory to be shared by two or more processes through page sharing. This leads to the following benefits: System libraries can be shared by several processes through mapping of the shared object into a virtual address space. Virtual memory enables processes to share memory. Virtual memory can allow pages to be shared during process creation with the forkO system call, thus speeding up process creation.
  • 9.
    DEMAND PAGING  Considerhow an executable program might be loaded from disk to memory.  One option, load the entire program in physical memory. Ex: User selection program  Second approach, load the pages only as they needed ( DEMAND PAGING).  A demand paging system is similar to the paging system with swapping.
  • 10.
    • Bring apage 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
  • 11.
    Transfer of aPaged Memory to Contiguous Disk Space
  • 12.
    Valid-Invalid Bit • Witheach page table entry a valid–invalid bit is associated (1  in-memory, 0  not-in-memory) • Initially valid–invalid bit 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
  • 13.
    Page Table WhenSome Pages Are Not in Main Memory
  • 14.
    Page Fault • Whathappens if the process tries to access a page and that page was not brought into memory?????? • 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
  • 15.
    Steps in Handlinga Page Fault
  • 16.
     The hardwareto support Demand paging:  Page Table  Secondary Memory
  • 17.
    What happens ifthere 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
  • 18.
    Performance of DemandPaging • Page Fault Rate 0  p  1.0 – if p = 0 no page faults – if p = 1, every reference is a fault • Effective Access Time (EAT) EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)
  • 19.
    Demand Paging Example Ifwe take an average page-fault service time of 8 milliseconds and a memory-access time of 200 nanoseconds, then the effective access time in nanoseconds is
  • 20.
    Copy-on-Write • Copy-on-Write (COW)allows both parent and child processes to initially share the same pages in memory If either process modifies a shared page, only then is the page copied • COW allows more efficient process creation as only modified pages are copied • Free pages are allocated from a pool of zeroedout pages