0% found this document useful (0 votes)
14 views

Unit 4 -Topic 6(Virtual Memory) (1)

Virtual memory is a storage scheme that creates the illusion of a larger main memory by utilizing secondary memory, allowing multiple processes to be loaded in parts rather than as whole entities. Demand paging is a technique that loads pages into main memory only when they are needed, improving CPU utilization and system throughput while managing page faults effectively. Copy-on-write is an optimization for process creation that allows parent and child processes to share pages until a write operation occurs, minimizing unnecessary duplication.

Uploaded by

jokosa5389
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit 4 -Topic 6(Virtual Memory) (1)

Virtual memory is a storage scheme that creates the illusion of a larger main memory by utilizing secondary memory, allowing multiple processes to be loaded in parts rather than as whole entities. Demand paging is a technique that loads pages into main memory only when they are needed, improving CPU utilization and system throughput while managing page faults effectively. Copy-on-write is an optimization for process creation that allows parent and child processes to share pages until a write operation occurs, minimizing unnecessary duplication.

Uploaded by

jokosa5389
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit-IV

Topic 6 : Virtual Memory

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.

Diagram showing virtual memory that is larger than physical memory

 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.

Figure : Shared library using virtual memory

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.

Performance of Demand Paging


To understand the impact of demand paging on system performance, we compute the effective
access time (EAT) for memory. Let:
 ma be the memory access time (10 to 200 ns).
 p be the probability of a page fault (0 ≤ p ≤ 1).
When no page faults occur, EAT is simply ma. If a page fault occurs, additional time is required to
handle it.
Let p be the probability of a page fault (0<= p <=1). We would expect p to be close to zero-that is,
we would expect to have only a few page faults. Then EAT =(1−𝑝)×𝑚𝑎+𝑝×page fault time
To compute the effective access time, we must know how much time is needed to service a page fault.
A page fault causes the following sequence to occur:
1.Trap to the operating system.
2.Save the user registers and process state.
3. Determine that the interrupt was a page fault.
4. Check that the page reference was legal and determine the location of the page on the disk Issue a
read from the disk to a free frame:
a. Wait in a queue for this device until the read request is serviced.
b. Wait for the device seek and/ or latency time.
c. Begin the transfer of the page to a free frame.
5.While waiting, allocate the CPU to some other user (CPU scheduling, optional).
6.Receive an interrupt from the disk I/0 subsystem (I/0 completed).
7.Save the registers and process state for the other user (if step 6 is executed).
8.Determine that the interrupt was from the disk
9.Correct the page table and other tables to show that the desired page is now in memory.
10.Wait for the CPU to be allocated to this process again.
11. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction.
Not all of these steps are necessary in every case. In any case, we are faced with three major
components of the page-fault service time
Service the page-fault interrupt.
Read in the page.
Restart the process.
An additional aspect of demand paging is the handling and overall use of swap space.Swap Space
Optimization can be done by
 Fast Swap Space: Allocated in large blocks, faster than file system.
 File System Paging: Initially demand pages from the file system; subsequently use swap space for
better throughput.
 Hybrid Approach: Pages binary files from the file system, overwrites when replaced, and uses swap
space for stack/heap. Used in Solaris and BSD UNIX.

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.

You might also like