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

Operating System 13

Uploaded by

Ankit Raj
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)
9 views

Operating System 13

Uploaded by

Ankit Raj
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/ 15

Virtual Memory Management

● Virtual memory is a space where large programs can be stored in the form of
pages while being executed and only the necessary pages or portions of
processes are loaded into the main memory.

● This technique is useful because it provides a large virtual memory for user
programs in the presence of a very small physical memory.

● Thus, virtual memory is a technique that allows the execution of processes that
are not entirely contained in the physical memory.

Paging and Segmentation

Comparing simple paging and simple segmentation on one hand with fixed and dynamic
partitioning on the other, is the foundation for a fundamental breakthrough in memory
management.

The two characteristics of paging and segmentation are the keys to this breakthrough:

1. All memory references within a process are logical addresses that are
dynamically translated into physical addresses at the run time.
2. A process may be broken up into a number of pieces, that is, pages or segments,
and these pieces need not be contiguously located in the main memory during
execution.

● If the preceding two characteristics are present, then it is not necessary that all of
the pages or all of the segments of a process be in the main memory during
execution.

● If the piece (segment or page) that holds the next instruction and the piece that
holds the next data are located in the main memory, then at least for that time,
execution may proceed.

Execution of a Process

● The operating system begins by bringing in only one or a few pieces, to include
the initial program piece and the initial data piece to which those instructions

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
refer to.

● The portion of a process that is actually in the main memory at any time is called
the ‘resident set’ of the process.

● As the process executes, things proceed smoothly as long as all memory


references are to locations that are in the resident set.

● If the processor encounters a logical address that is not in the main memory, it
generates
an interrupt, indicating a memory access fault.

● The operating system puts the interrupted process in a blocking state.

● For the execution of this process to proceed later, the operating system must
bring into the main memory the piece of the process that contains the logical
address that was responsible for the access fault.

● The operating system issues a disk I/O (input/output) read request.

● After the I/O request has been issued, the operating system can dispatch another
process to run while the disk I/O is performed.

● Once the desired piece has been brought into the main memory, an I/O interrupt
is issued, giving the control back to the operating system, which places the
affected process back into a ready state.

There are two implications of the new strategy:

1) More processes may be maintained in the main memory.


● Only some of the pieces of any particular process are to be loaded, there
is room for more processes.

● This leads to more efficient utilisation of the processor because it is more


likely that at least one of the more numerous processes will be in a Ready
state at any particular time.

2) A process may be larger than all of the main memory.


● One of the most fundamental restrictions in programming is lifted. A programmer

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
must be acutely aware of how much memory is available.

● If the program being written is too large, the programmer must devise ways to
structure the program into pieces that can be loaded separately in some sort of
overlay strategy.

● With virtual memory being based on paging or segmentation, that job is left to the
operating system and the hardware.

● The operating system automatically loads the pieces of a process into the main
memory as required.

Real and Virtual Memory

● A process executes only in the main memory. That memory is referred to as ‘real
memory’. But a programmer or user perceives a potentially much larger memory,
which is allocated on the disk.

● This is referred to as ‘virtual memory’. Virtual memory allows for very effective
multiprogramming and relieves the user of the unnecessarily tight constraints of
the main memory.

Locality and Virtual memory

● At one time, there was a considerable debate on this point, but experience with
numerous operating systems has demonstrated, beyond doubt, that virtual
memory does work.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● Accordingly, virtual memory, based on either paging or paging plus
segmentation, has become an essential component of contemporary operating
systems.

● Let’s understand the key issue and why virtual memory was a matter of much
debate.

● Consider a large process, comprising a long program plus a number of arrays of


data. Over any short period of time, execution may be confined to a small section
of the program (such as a subroutine) and access to perhaps only one or two
arrays of data.

● If this is so, then it would clearly be wasteful to load in dozens of pieces for that
process when only a few pieces will be used before the program is suspended
and swapped out.

● Then, if the program branches to instruction or references a data item on a piece


not in the main memory, a fault is triggered.

● Thus, at any one time, only a few pieces of any given process are in the memory,
and therefore, more processes can be maintained in the memory.

● Furthermore, time is saved because unused pieces are not swapped in and out
of the memory.

Thrashing

● In the steady state, practically all of the main memory will be occupied with
process pieces, so the processor and the operating system have direct access to
as many processes as possible.

● Thus, when the OS brings one piece in, it must throw another out. If it throws out
a piece just before it is used, then it will just have to go get that piece again
almost immediately. Too much of this leads to a condition known as ‘thrashing’.

● Thrashing means the system spends most of its time swapping pieces rather
than executing instructions.

● To avoid this, the operating system tries to guess, based on recent history, which
pieces are least likely to be used in the near future.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● This reasoning is based on a belief in the principle of locality.

Principle of Locality

● The principle of locality states that program and data references within a process
tend to cluster.

● Only a few pieces of a process that will be needed over a short period of time are
valid.

● The principle of locality suggests that a virtual memory scheme may be effective.
For virtual memory to be practical and effective, two ingredients are needed.

1) There must be hardware support for the paging and/or segmentation scheme.

2) The operating system must include software to manage the movement of pages
and/or segments between the secondary memory and the main memory.

Characteristics of Paging and Segmentation vs Virtual Paging and Virtual


Segmentation

● The term virtual memory is usually associated with systems that employ paging,
although virtual memory based on segmentation is also used.

● The main memory is divided into a number of equal-sized frames.

● Each process is divided into a number of equal-sized pages of the same length

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
as the frames.

● A process is loaded by loading all of its pages into the available, but not
necessarily contiguous, frames.

● With virtual memory paging, you again have equal-sized pages of the same
length as the frames; however, not all pages need to be loaded into the main
memory frames for execution.

Virtual Memory Paging

● Each process has its own page table, and when all of its pages are loaded into
the main memory, the page table for a process is created and loaded into the
main memory.

● Each page table entry (PTE) contains the frame number of the corresponding
page in the main memory.

● A page table is also needed for a virtual memory scheme based on paging.

● The page table entries become more complex. Because only some of the pages
of a process may be in the main memory, a bit is needed in each page table

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
entry to indicate whether the corresponding page is present in the main memory
or not (character P).

● If P is 1, then the page is in memory. Hence the entry would also have the frame
number of that page.

● The page table entry includes a modify (M) bit, indicating whether the contents of
the corresponding page have been altered since the page was last loaded into
the main memory.

● If there has been no change, then it is not necessary to write the page out when
it comes time to replace the page in the frame that it currently occupies.

● Other control bits may also be present. For example, if protection or sharing is
managed at the page level, then bits for that purpose will be required.

Translation Lookaside Buffer (TLB)

● Every virtual memory reference has two physical memory accesses:

○ One to fetch the appropriate page table entry and


○ Another to fetch the desired data.

● Thus, a straightforward virtual memory scheme would have the effect of doubling
the memory access time.

● To overcome this problem, most virtual memory schemes make use of a special
high-speed cache for page table entries, called ‘translation lookaside buffer’ or
simply ‘TLB’.

● This cache functions in the same way as a memory cache and contains the most
recently used page table entities.

● The diagram below depicts the use of a translation lookaside buffer.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● Given a virtual address, the processor will first examine the translation lookaside
buffer.

● If the desired page table entry is present, which is a translation lookaside buffer
hit, then the frame number is retrieved and the real address is formed.

● If the desired page table entry is not found, which is a translation lookaside buffer
miss, then the processor uses the page number to index the process page table
and examines the corresponding page table entry.

● If the ‘present bit’ is set, then the page is in the main memory and the processor
can retrieve the frame number from the page table entry to form the real address.

● The processor also updates the translation lookaside buffer to include this new
page table entry.

● Finally, if the present bit is not set, then the desired page is not in the main
memory and a memory access fault, called a page fault, is issued.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● At this point, you invoke the operating system, which loads the needed page and
updates the page table.

● The diagram given below is a flowchart that shows the use of TLB

● The flowchart shows that if the desired page is not in the main memory, a page
fault interrupt invokes the page fault handling routine.

● By the principle of locality, most virtual memory references will be to locations in


recently used pages. Therefore, most references will involve page table entries in
the cache.

● Because the translation lookaside buffer contains only some of the entries in a
full page table, you cannot simply index into the translation lookaside buffer
based on the page number.

● Each entry in the translation lookaside buffer must include the page number as
well as the complete page table entry.

● The processor is equipped with hardware that allows it to interrogate several

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
translation lookaside buffer entries simultaneously to determine if there is a
match on the page number.

● This technique is referred to as associative mapping and it is different from direct


mapping, or indexing, which is used for lookup in the page table.

Translation Lookaside Buffer and Cache Operation

● The translation lookaside buffer must also be designed considering the way in
which entries are organised in the translation lookaside buffer and which entry to
replace when a new entry is brought in. These issues must be considered in any
hardware cache design.

● The virtual memory mechanism must interact with the cache system (not the
translation lookaside buffer cache, but the main memory cache).

● A virtual address is typically composed of a page number and an offset.

● To begin, the memory system checks the translation lookaside buffer to see if the
matching page table entry exists.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● If it does, the real or physical address is calculated by multiplying the frame
number by the offset.

● If this is not the case, the entry is retrieved from a page table.

● Once the real address, which is in the form of a tag and a remainder, is
generated, the cache is queried to see if the block containing that word is
present.

● If this is the case, it is returned to the CPU. If this is not the case, the word is
retrieved from the main memory.

Virtual Memory Segmentation

Segmentation allows the programmer to view memory as comprising multiple address


spaces or segments. Segments may be of unequal, indeed dynamic, sizes. Memory
references comprise a (segment number, offset) form of address.

This organisation confers the following advantages to the programmer over a non-
segmented address space:

1) It simplifies the handling of the growing data structures.

2) It allows programs to be altered and recompiled independently, without requiring


the entire set of programs to be relinked and reloaded.

3) It lends itself to sharing among processes. A programmer can place a utility


program or a useful table of data in a segment that can be referenced by other
processes.

4) It lends itself to protection. Because a segment can be constructed to contain a


well-defined set of programs or data, the programmer or system administrator
can assign access privileges in a convenient fashion.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● The basic mechanism for reading a word from memory involves the translation of
a virtual, or logical, address, comprising a segment number and an offset, into a
physical address, using a segment table.
● Because the segment table's length varies depending on the size of the process,
it cannot be held in registers.

● Instead, it must be in the main memory to be accessed. The figure above depicts
a hardware implementation of this scheme.

● When a particular process is running, a register holds the starting address of the
segment table for that process.

● The segment number of a virtual address is used to index that table and look up
the corresponding main memory address for the start of the segment.

● This is added to the offset portion of the virtual address to obtain the desired real
address.

Address translation in a combined segmentation/paging system

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
● In a combined paging/segmentation system, a user’s address space is broken up
into several segments at the discretion of the programmer.

● Each segment is, in turn, broken up into a number of fixed-sized pages, which
are equal in length to the main memory frame.

● If a segment has a length less than that of a page, the segment occupies just one
page.

● From the programmer’s point of view, a logical address still comprises a segment
number and a segment offset.

● From the system’s point of view, the segment offset is viewed as a page number
and the page offset for a page within the specified segment.

● Associated with each process is a segment table and several page tables, one
per process segment.

● When a particular process is running, a register holds the starting address of the
segment table for that process.

● Presented with a virtual address, the processor uses the segment number
portion to index into the process segment table to find the page table for that

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
segment.

● Then, the page number portion of the virtual address is used to index the page
table and look up the corresponding frame number.

● This is combined with the offset portion of the virtual address to produce the
desired real address.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
Powered by upGrad Education Private Limited
© Copyright. UpGrad Education Pvt. Ltd. All rights reserved
Disclaimer: All content and material on the upGrad website is copyrighted material,
either belonging to upGrad or its bonafide contributors and is purely for the
dissemination of education. You are permitted to access print and download extracts
from this site purely for your own education only and on the following basis:

• You can download this document from the website for self-use only.
• Any copies of this document, in part or full, saved to disc or to any other storage
medium may only be used for subsequent, self-viewing purposes or to print an
individual extract or copy for non-commercial personal use only.
• Any further dissemination, distribution, reproduction, copying of the content of the
document herein or the uploading thereof on other websites or use of the
content for any other commercial/unauthorised purposes in any way which could
infringe the intellectual property rights of upGrad or its contributors, is strictly
prohibited.
• No graphics, images or photographs from any accompanying text in this
document will be used separately for unauthorised purposes.
• No material in this document will be modified, adapted or altered in any way.
• No part of this document or upGrad content may be reproduced or stored in any
other web site or included in any public or private electronic retrieval system or
service without upGrad’s prior written permission.
• Any rights not expressly granted in these terms are reserved.

Powered by upGrad Education Private Limited


© Copyright. UpGrad Education Pvt. Ltd. All rights reserved

You might also like