21-VM-instructor - Rose

Day 21
Virtual Memory
Assumptions


An entire process resides in main memory.
A process resides in main memory as one
contiguous block.

With the help of page tables/segment tables, a
process can be split up into portions
(page/segment).


Hardware allows us to place and then relocate a process anywhere in memory.
Also, hardware makes it possible to place
the process portions in non-contiguous
locations.
Assumption

An entire process need not be held in
memory.


Portions of the process that are needed will be
placed in memory.
Portions are brought in as they are needed.
What happens when a new portion
is needed?
The resident set(i.e. the pieces of the program that currently reside in
main memory) must consist of the first address to be accessed and
the first data to be accessed.
The first instruction’s execution takes place.









Address translations are performed using the page/segment table.
At some point, the instruction to be accessed or the data to be
accessed will not be available in main memory.
The hardware will issue an interrupt.
The OS will take over. The current process will get blocked.
The OS will issue a disk request on behalf of the process.
When the disk read is complete, the block is moved from secondary
memory to main memory and another interrupt is now issued by the
disk indicating that the service has been performed.
Process will be moved back to the Ready queue. When process gets
scheduled to run, it will continue.
Advantages

More processes in main memory.


Better utilization of the processor.
A process larger than main memory can also
be run, with no intervention from the user.

Only the size of secondary memory limits the
user.
Real memory/Virtual memory


Real memory – the main memory space.
Virtual memory – the space perceived by the
user

includes secondary storage
Locality of reference

Data and instruction references tend to
cluster

So, at any given time, only some part of the
process has to reside in main memory.
Which block to replace when main
memory is full?



If incorrect decisions are made, then the
blocks swapped out will have to be brought
back again
Thrashing - The OS will spend a large
amount of time swapping blocks in and out of
main memory
Must make intelligent choices about
which block to swap out to minimize
thrashing.
Virtual memory works
because:


The OS can make intelligent guesses about
which blocks must be swapped out.
Hardware can keep track of which portions of
a process are in main memory and also
where they are in main memory.
Virtual memory with paging
Page table per process.
Page table entry includes






Page tables can be huge




present bit
frame number
modify bit
flags for protection and sharing.
an entry per page of the process
large processes (larger than main memory).
Page table itself is placed in virtual memory.


For a program to execute, the relevant
portion of the page table and the starting
instructions and data of the process must be
present in main memory.
A register holds the address of the page table
in main memory.
Also in main memory
Simple indexing
Two-level scheme to support large
tables
Consider a process as described here:
 Logical address space is 4-GiB (232 bytes)
 Size of a page is 4KiB (212 bytes)
 There are ___ pages in the process.


If each page table entry occupies 4-bytes, then need ______ byte large
page table




This implies we need ____ page table entries.
The page table will occupy __________ pages.
Root table will consist of ____ entries – one for each page that holds
part of the page table.
Root table will occupy 212 bytes i.e 4KiB of space and will be kept in
main memory permanently.
A page access could require two disk accesses.
Two-level scheme to support large
tables
Consider a process as described here:
 Logical address space is 4-GiB (232)
 Size of a page is 4KiB (212 bytes)
 There are 220 pages in the process. (232/212)
 This implies we need 220 page table entries.
 If each page table entry occupies 4-bytes, then need 222 byte
(4MiB) large page table

The page table will occupy 222/212 i.e. 210 pages.
 Root table will consist of 210 entries – one for each page that
holds a page table.
 Root table will occupy 212 bytes i.e 4KiB of space and will be
kept in main memory permanently.
 Could require two disk accesses.