Unit 4 -Topic 6(Virtual Memory) (1)
Unit 4 -Topic 6(Virtual Memory) (1)
Suppose you have a furniture shop that is not big enough to accommodate all the furniture, then
you will keep it somewhere else, like in a warehouse. And you will keep that furniture in your shop
which will be in demand. And if you don’t require some particular furniture, you will keep that in the
warehouse. And according to trend or demand, you will keep exchanging the furniture items from
the warehouse. The same concept is for virtual memory.(warehouse is main memory and furniture’s
are process).
Virtual Memory is a storage scheme that provides user an illusion of having a very big main memory.
This is done by treating a part of secondary memory as the main memory. In this scheme, User can
load the bigger size processes than the available main memory by having the illusion that the
memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System loads the different
parts of more than one process in the main memory. By doing this, the degree of multiprogramming
will be increased and therefore, the CPU utilization will also be increased.
In practice, most real processes do not need all their pages, or at least not all at once, for
several reasons:
Error handling code is not needed unless that specific error occurs, some of which are quite
rare.
Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays
are actually used in practice.
The ability to load only the portions of processes that were actually needed ( and only when they
were needed ) has several benefits:
Programs could be written for a much larger address space ( virtual memory space ) than
physically exists on the computer.
Because each process is only using a fraction of their total address space, there is more
memory left for other programs, improving CPU utilization and system throughput.
Less I/O is needed for swapping processes in and out of RAM, speeding things up.
Figure below shows virtual address space, which is the programmers logical view of process
memory storage. The actual physical layout is controlled by the process's page table.
Note that the address space shown in Figure is sparse - A great hole in the middle of the address
space is never used, unless the stack and/or the heap grow to fill the hole.
Virtual memory also allows the sharing of files and memory by multiple processes, with several
benefits:
System libraries can be shared by mapping them into the virtual address space of more than one
process.
Processes can also share virtual memory by mapping the same block of memory to more than one
process.
Process pages can be shared during a fork( ) system call, eliminating the need to copy all of the pages
of the original ( parent ) process.
Demand Paging
According to the concept of Virtual Memory, in order to execute some process, only a part
of the process needs to be present in the main memory which means that only a few pages will only
be present in the main memory at any time.
However, deciding, which pages need to be kept in the main memory and which need to be
kept in the secondary memory, is going to be difficult because we cannot say in advance that a
process will require a particular page at particular time.
Therefore, to overcome this problem, there is a concept called Demand Paging is introduced.
It suggests keeping all pages of the frames in the secondary memory until they are required. In other
words, it says that do not load any page in the main memory until it is required. Whenever any page
is referred for the first time in the main memory, then that page will be found in the secondary
memory.
The demand paging system is somehow similar to the paging system with swapping where
processes mainly reside in the main memory(usually in the hard disk). Thus demand paging is the
process that solves the above problem only by swapping the pages on Demand.
This is also known as lazy swapper( It never swaps the page into the memory unless it is
needed).Swapper that deals with the individual pages of a process are referred to as Pager.
Some form of hardware support is used to distinguish between the pages that are in the memory and
the pages that are on the disk. Thus for this purpose Valid-Invalid scheme is used:
With each page table entry, a valid-invalid bit is associated( where 1 indicates in the
memory and 0 indicates not in the memory)
Initially, the valid-invalid bit is set to 0 for all table entries.
1. If the bit is set to "valid", then the associated page is both legal and is in memory.
2. If the bit is set to "invalid" then it indicates that the page is either not valid or the page is
valid but is currently not on the disk.
For the pages that are brought into the memory, the page table is set as usual.
But for the pages that are not currently in the memory, the page table is either simply marked
as invalid or it contains the address of the page on the disk.
During the translation of address, if the valid-invalid bit in the page table entry is 0 then it leads to page
fault.
The above figure is to indicates the page table when some pages are not in the main memory.
If the referred page is not present in the main memory then there will be a miss and the concept
is called Page miss or page fault. Let us understand the procedure to handle the page fault as
shown with the help of the below diagram:
1. First of all, internal table(that is usually the process control block) for this process in order
to determine whether the reference was valid or invalid memory access.
2. If the reference is invalid, then we will terminate the process. If the reference is valid, but
we have not bought in that page so now we just page it in.
3. Then we locate the free frame list in order to find the free frame.
4. Now a disk operation is scheduled in order to read the desired page into the newly allocated
frame.
5. When the disk is completely read, then the internal table is modified that is kept with the
process, and the page table that mainly indicates the page is now in memory.
6. Now we will restart the instruction that was interrupted due to the trap. Now the process
can access the page as though it had always been in memory.
In some cases when initially no pages are loaded into the memory, pages in such cases are only
loaded when are demanded by the process by generating page faults. It is then referred to as Pure
Demand Paging.
Copy-on-Write
Copy-on-Write Recall that the fork( ) system call creates a child process that is a duplicate of its parent.
Traditionally, fork( ) worked by creating a copy of the parent’s address space for the child, duplicating
the pages belonging to the parent. However, considering that many child processes invoke the exec()
system call immediately after creation, the copying of the parent’s address space may be unnecessary.
Instead, we can use a technique known as copy-on-write, which works by allowing the parent and child
processes initially to share the same pages. These shared pages are marked as copy-on-write pages,
meaning that if either process writes to a shared page, a copy of the shared page is created. Copy-on-
write is illustrated in Figures below, which show the contents of the physical memory before and after
process 1 modifies page C
When it is determined that a page is going to be duplicated using copy on-write, it is important to
note the location from which the free page will be allocated. Many operating systems provide a pool
of free pages for such requests. These free pages are typically allocated when the stack or heap for
a process must expand or when there are copy-on-write pages to be managed. Operating systems
typically allocate these pages using a technique known as Zero-fill-on-demand. Zero-fill-on-demand
pages have been zeroed-out before being allocated, thus erasing the previous contents.