0% found this document useful (0 votes)
29 views30 pages

Virtual Memory

Uploaded by

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

Virtual Memory

Uploaded by

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

Virtual Memory

It is a technique which allows execution of process that may not be


compiled within the primary memory.
It separates the user logical memory from the physical memory. This
separation allows an extremely large memory to be provided for
program when only a small physical memory is available.
Virtual memory makes the task of programming much easier because the
programmer no longer needs to working about the amount of the
physical memory is available or not.
The virtual memory allows files and memory to be shared by
different processes by page sharing.
It is most commonly implemented by demand paging.

70 | P a g e
Demand Paging
A demand paging system is similar to the paging system with swapping
feature. When we want to execute a process we swap it into the memory. A
swapper manipulates entire process where as a pager is concerned with the
individual pages of a process. The demand paging concept is using pager
rather than swapper. When a process is to be swapped in, the pager guesses
which pages will be used before the process is swapped out again. Instead of
swapping in a whole process, the pager brings only those necessary pages
into memory. The transfer of a paged memory to contiguous disk space
is shown in below figure.

Thus it avoids reading into memory pages that will not used any way
decreasing the swap time and the amount of physical memory needed. In
this technique we need some hardware support to distinct between the
pages that are in memory and those that are on the disk. A valid and invalid
bit

71 | P a g e
is used for this purpose. When this bit is set to valid it indicates that the
associate page is in memory. If the bit is set to invalid it indicates that the
page is either not valid or is valid but currently not in
the disk.

71 | P a g e
Marking a page invalid will have no effect if the process never attempts to
access that page. So while a process executes and access pages that are
memory resident, execution proceeds normally. Access to a page marked
invalid causes a page fault trap. It is the result of the OS’s failure to bring
the desired page into memory.
Procedure to handle page fault
If a process refers to a page that is not in physical memory then
We check an internal table (page table) for this process to determine
whether the reference was valid or invalid.
If the reference was invalid, we terminate the process, if it was valid
but not yet brought in, we have to bring that from main memory.
Now we find a free frame in memory.
Then we read the desired page into the newly allocated frame.
When the disk read is complete, we modify the internal table to indicate
that the page is now in memory.
We restart the instruction that was interrupted by the illegal address
trap. Now the process can access the page as if it had always been in
memory.
Page Replacement

7272 | P a g
e
Each process is allocated frames (memory) which hold the
process’s pages (data) Frames are filled with pages as
needed – this is called demand paging

7373 | P a g
e
Over-allocation of memory is prevented by modifying the page-fault
service routine to replace pages
The job of the page replacement algorithm is to decide which page
gets victimized to make room for a new page
Page replacement completes separation of logical and physical memory
Page Replacement Algorithm
Optimal algorithm
Ideally we want to select an algorithm with the lowest page-fault rate
Such an algorithm exists, and is called (unsurprisingly) the optimal
algorithm:
Procedure: replace the page that will not be used for the longest time
(or at all) – i.e. replace the page with the greatest forward distance in the
reference string
Example using 4 frames:

Reference # 1 2 3 4 5 6 7 8 9 1 1 1
0 1 2
Page 1 2 3 4 1 2 5 1 2 3 4 5
referenced
Frames 1 1 1 1 1 1 1 1 1 1 4 4
_ = faulting 2 2 2 2 2 2 2 2 2 2 2
page
3 3 3 3 3 3 3 3 3 3
4 4 4 5 5 5 5 5 5

Analysis: 12 page references, 6 page faults, 2 page replacements.


Page faults per number of frames = 6/4 = 1.5
Unfortunately, the optimal algorithm requires special hardware (crystal
ball, magic mirror, etc.)
not typically found on today’s computers
Optimal algorithm is still used as a metric for judging other page
replacement algorithms
FIFO algorithm
Replaces pages based on their order of arrival: oldest page is replaced
Example using 4 frames:

7474 | P a g
e
Reference # 1 2 3 4 5 6 7 8 9 1 1 1
0 1 2
Page 1 2 3 4 1 2 5 1 2 3 4 5
referenced
Frames 1 1 1 1 1 1 5 5 5 5 4 4
_ = faulting 2 2 2 2 2 2 1 1 1 1 5
page
3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 3 3 3

Analysis: 12 page references, 10 page faults, 6 page replacements.


Page faults per number of frames = 10/4 = 2.5
LFU algorithm (page-
based)
procedure: replace the page which has been
referenced least often
For each page in the reference string, we need to keep a reference
count. All reference counts start at 0 and are incremented every time a
page is referenced.
example using 4
frames:

Reference # 1 2 3 4 5 6 7 8 9 1 1 1
0 1 2
Page 1 2 3 4 1 2 5 1 2 3 4 5
referenced 1 1 1 1 2 2 2 3 3 3 3 3
Frames 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 2 2 2 3 3 3 3
_ = faulting 2 2 2 2 2 2 2 2 2 2 2
page 1 1 1 1 1 1 1 2 2 2
3 3 3 3 5 5 5 3 3 5
n
= reference 1 1 1 1 1 1 1 2 2
4 4 4 4 4 4 4 4 4
count

At the 7th page in the reference string, we need to select a page to be


victimized. Either 3 or 4 will do since they have the same reference
count (1). Let’s pick 3.
Likewise at the 10th page reference; pages 4 and 5 have been
referenced once each. Let’s pick page 4 to victimize. Page 3 is brought

7575 | P a g
e
in, and its reference count (which was 1 before we paged it out a while
ago) is updated to 2.
Analysis: 12 page references, 7 page faults, 3 page replacements.
Page faults per number of frames = 7/4 = 1.75

7676 | P a g
e
LFU algorithm (frame-
based)
Procedure: replace the page in the frame which has been
referenced least often
Need to keep a reference count for each frame which is initialized to 1
when the page is paged in, incremented every time the page in the
frame is referenced, and reset every time the page in the frame is
replaced
Example using 4
frames:

Reference # 1 2 3 4 5 6 7 8 9 1 1 1
0 1 2
Page 1 2 3 4 1 2 5 1 2 3 4 5
referenced
Frames 1 1 1 1 2 2 2 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 2 2 2 3 3 3 3
_ = faulting 2 2 2 2 2 2 2 2 2 2 2
page 1 1 1 1 1 1 1 1 1 1
3 3 3 3 5 5 5 3 3 5
n
= reference 1 1 1 1 1 1 1 2 2
4 4 4 4 4 4 4 4 4
count
At the 7th reference, we victimize the page in the frame which has been
referenced least often -- in this case, pages 3 and 4 (contained within
frames 3 and 4) are candidates, each with a reference count of 1.
Let’s pick the page in frame 3. Page 5 is paged in and frame 3’s
reference count is reset to 1.
At the 10th reference, we again have a page fault. Pages 5 and 4
(contained within frames 3 and
4) are candidates, each with a count of 1. Let’s pick page 4. Page 3 is
paged into frame 3, and frame 3’s reference count is reset to 1.
Analysis: 12 page references, 7 page faults, 3 page replacements.
Page faults per number of frames = 7/4 = 1.75
LRU algorithm

7777 | P a g
e
Replaces pages based on their most recent reference – replace the
page with the greatest backward distance in the reference string
Example using 4
frames:

Reference # 1 2 3 4 5 6 7 8 9 1 1 1
0 1 2

7878 | P a g
e
Page 1 2 3 4 1 2 5 1 2 3 4 5
referenced
Frames 1 1 1 1 1 1 1 1 1 1 1 5
_ = faulting 2 2 2 2 2 2 2 2 2 2 2
page
3 3 3 3 5 5 5 5 4 4
4 4 4 4 4 4 3 3 3

Analysis: 12 page references, 8 page faults, 4 page replacements.


Page faults per number of frames = 8/4 = 2
One possible implementation (not necessarily the best):
o Every frame has a time field; every time a page is referenced, copy
the current time into its frame’s time field
o When a page needs to be replaced, look at the time stamps to find
the oldest
Thrashing
• If a process does not have “enough” pages, the page-fault rate is very
high
– low CPU utilization
– OS thinks it needs increased multiprogramming
– adds another process to system
• Thrashing is when a process is busy swapping pages in and out
• Thrashing results in severe performance problems. Consider the
following scenario, which is based on the actual behaviour of early
paging systems. The operating system monitors CPU utilization. If
CPU utilization is too low, we increase the degree of
multiprogramming by introducing a new process to the system. A
global page replacement algorithm is used; it replaces pages with no
regard to the process to which they belong. Now suppose that a
process enters a new phase in its execution and needs more frames.

7979 | P a g
e
FILE SYSTEM
File concept:
A file is a collection of related information that is stored on secondary
storage. Information stored in files must be persistent i.e. not affected by
power failures & system reboots. Files may be of free
from such as text files or may be formatted rigidly. Files represent both
programs as well as data. Part of the OS dealing with the files is known as
file system. The important file concepts include:
1. File attributes: A file has certain attributes which vary from one
operating system to another.
Name: Every file has a name by which it is referred.
Identifier: It is unique number that identifies the file within the file
system.
Type: This information is needed for those systems that support
different types of files.
Location: It is a pointer to a device & to the location of the file on that
device
Size: It is the current size of a file in bytes, words or blocks.
Protection: It is the access control information that determines who
can read, write &
execute a file.
Time, date & user identification: It gives information about
time of creation or last modification & last use.
2. File operations: The operating system can provide system calls to
create, read, write, reposition,
delete and truncate files.

8080 | P a g
e
Creating files: Two steps are necessary to create a file. First, space
must be found for the file in the file system. Secondly, an entry must
be made in the directory for the new file. Reading a file: Data &
read from the file at the current position. The system must keep a
read pointer to know the location in the file from where the next read
is to take place. Once
the read has been taken place, the read pointer is updated.

8181 | P a g
e
Writing a file: Data are written to the file at the current position.
The system must keep a write pointer to know the location in the file
where the next write is to take place. The write pointer must be
updated whenever a write occurs.
Repositioning within a file (seek): The directory is searched for
the appropriate entry & the current file position is set to a given
value. After repositioning data can be read from or written into that
position.
Deleting a file: To delete a file, we search the directory for the
required file. After deletion, the space is releasedso that it can be
reused by other files.
Truncating a file: The user may erase the contents of a file but
allows all attributes to remain unchanged expect the file length
which is rest to ‘O’ & the space is released.
3. File types: The file name is spilt into 2 parts, Name & extension.
Usually these two parts are separated by a period. The user & the OS can
know the type of the file from the extension itself. Listed below are some
file types along with their extension:
File Type
Extension Executable File
exe, bin, com Object File
obj, o (compiled) Source Code
file C, C++, Java, pas
Batch File bat, sh (commands to command
the interpreter) Text File txt, doc (textual
data documents)
arc, zip, tar (related files grouped together into file
compressed for
Archieve storage)
File
Multimedia File mpeg (Binary file containing audio or A/V
information)

8282 | P a g
e
4. File structure: Files can be structured in several ways. Three common
possible are:
Byte sequence:The figure shows an unstructured sequence of bytes.
The OS doesn’t care about the content of file. It only sees the bytes.
This structure provides maximum flexibility. Users can write anything
into their files & name them according to their convenience. Both
UNIX & windows use this approach.

byte

8383 | P a g
e
Record sequence: In this structure, a file is a sequence of fixed
length records. Here the
read operation returns one records & the write operation overwrites
or append or record.

Record

Tree:In this organization, a file consists of a tree of records of


varying lengths. Each record consists of a key field. The tree is stored
on the key field to allow first searching for a particular key.
Access methods: Basically, access method is divided into 2 types:
Sequential access: It is the simplest access method. Information in
the file is processed in order i.e. one record after another. A process
can read all the data in a file in order starting from beginning but
can’t skip & read arbitrarily from any location. Sequential files can be
rewound. It is convenient when storage medium was magnetic tape
rather than disk.
Direct access: A file is made up of fixed length-logical records that
allow programs to read
& write records rapidly in no particular O order. This method can be
used when disk are used for storing files. This method is used in many
applications e.g. database systems. If an airline customer wants to
reserve a seat on a particular flight, the reservation program must be

8484 | P a g
e
able to access the record for that flight directly without reading the
records before it. In a direct access file, there is no restriction in the
order of reading or writing. For example, we can read block 14, then
read block 50 & then write block 7 etc. Direct access files are very
useful for immediate access to large amount of information.

8585 | P a g
e
Directory structure: The file system of computers can be extensive. Some
systems store thousands of file on disk. To manage all these data, we need
to organize them. The organization is done in 2 steps. The file system is
broken into partitions. Each partition contains information about file within
it.
Operation on a directory:
Search for a file: We need to be able to search a directory for a
particular file.
Create a file: New files are created & added to the directory.
Delete a file: When a file is no longer needed, we may remove it from
the directory.
List a directory: We should be able to list the files of the directory.
Rename a file: The name of a file is changed when the contents
of the file changes. Traverse the file system: It is useful to be
able to access every directory & every file within a directory.
Structure of a directory: The most common schemes for defining the
structure of the directory
are:
1. Single level directory: It is the simplest directory structure. All
files are present in the same directory. So it is easy to manage &
understand.
Limitation: A single level directory is difficult to manage when the
no. of files increases or when there is more than one user. Since all
files are in same directory, they must have unique names. So, there is
confusion of file names between different users.
2. Two level directories: The solution to the name collision problem in
single level directory is to
create a separate directory for each user. In a two level directory
structure, each user has its own user file directory. When a user logs in,
then master file directory is searched. It is indexed by user name & each
entry points to the UFD of that user.
Limitation: It solves name collision problem. But it isolates one user from
another. It is an
8080 | P a g
e
advantage when users are completely independent. But it is a
disadvantage when the users need to access each other’s files & co-
operate among themselves on a particular task.
3. Tree structured directories: It is the most common directory structure.
A two level directory is
a two level tree. So, the generalization is to extend the directory
structure to a tree of arbitrary height. It allows users to create their
own subdirectories & organize their files. Every file in the system has a
unique path name. It is the path from the root through all the sub-
directories to a specified file. A directory is simply another file but it is
treated in a special way. One bit in each

8181 | P a g
e
directory entry defines the entry as a file (O) or as sub- directories. Each
user has a current directory. It contains most of the files that are of
current interest to the user. Path names can be of two types: An absolute
path name begins from the root directory & follows the path down to the
specified files. A relative path name defines the path from the current
directory. E.g. If the current directory is root/spell/mail, then the relative
path name is prt/first & the absolute path name is root/ spell/ mail/ prt/
first. Here users can access the files of other users also by specifying
their path names.
4. A cyclic graph directory:It is a generalization of tree structured
directory scheme. An a cyclic
graph allows directories to have shared sub-directories & files. A shared
directory or file is not the same as two copies of a file. Here a
programmer can view the copy but the changes made in the file by one
programmer are not reflected in the other’s copy. But in a shared file,
there is only one actual file. So many changes made by a person would
be immediately visible to others. This scheme is useful in a situation
where several people are working as a team. So, here all the files that
are to be shared are put together in one directory. Shared files and sub-
directories can be implemented in several ways. A common way used in
UNIX systems is to create a new directory entry called link. It is a
pointer to another file or sub-directory. The other approach is to
duplicate all information in both sharing directories. A cyclic graph
structure is more flexible then a tree structure but it is also more
complex.
Limitation: Now a file may have multiple absolute path names. So,
distinct file names may refer to the same file. Another problem occurs
during deletion of a shared file. When a file is removed by any one user. It
may leave dangling pointer to the non existing file. One serious problem
in a cyclic graph structure is ensuring that there are no cycles. To avoid
these problems, some
systems do not allow shared directories or files. E.g. MS-DOS uses a tree
structure rather than a cyclic to avoid the problems associated with
deletion. One approach for deletion is to preserve the file until all
references to it are deleted. To implement this approach, we must have
some mechanism for determining the last reference to the file. For this
we have to keep a list of reference to a file. But due to the large size of
the no. of references. When the count is zero, the file can be deleted.
5. General graph directory: When links are added to an existing tree
structured directory, the tree structure is destroyed, resulting in a
simple graph structure. Linking is a technique that
allows a file to appear in more than one directory. The advantage is the
simplicity of algorithm to
transverse the graph & determines when there are no more references to
a file. But a similar

81 | P a g e
problem exists when we are trying to determine when a file can be
deleted. Here also a value
zero in the reference count means that there are no more references to
the file or directory & the file can be deleted. But when cycle exists, the
reference count may be non-zero even when there are no references to
the directory or file. This occurs due to the possibility of self referencing
(cycle) in the structure. So, here we have to use garbage collection
scheme to determine when
the last references to a file has been deleted & the space can be
reallocated. It involves two steps:
Transverse the entire file system & mark everything
that can be accessed. Everything that isn’t marked is
added to the list of free space.
But this process is extremely time consuming. It is only necessary due to
presence of cycles in
the graph. So, a cyclic graph structure is easier to work than this.
Protection
When information is kept in a computer system, a major concern is its
protection from physical damage (reliability) as well as improper access.
Types of access: In case of systems that don’t permit access to the files of
other users. Protection is not needed. So, one extreme is to provide
protection by prohibiting access. The other extreme is to provide free
access with no protection. Both these approaches are too extreme for
general use. So, we need controlled access. It is provided by limiting the
types of file access. Access is permitted depending on several factors. One
major factor is type of access requested. The different type of operations
that can be controlled are:
Read
Write
Execu
te
Appen
d
Delete
List
Access lists and groups:
Various users may need different types of access to a file or directory. So,
we can associate an access lists with each file and directory to implement
identity dependent access. When a user access requests access to a
particular file, the OS checks the access list associated with that file. If that
user is granted the requested access, then the access is allowed. Otherwise,
a protection violation occurs
& the user is denied access to the file. But the main problem with access lists
is their length. It is

82 | P a g e
very tedious to construct such a list. So, we use a condensed version of the
access list by classifying the users into 3 categories:
Owners: The user who created the file.
Group: A set of users who are sharing the files.
Others: All other users in the system.
Here only 3 fields are required to define protection. Each field is a
collection of bits each of which either allows or prevents the access. E.g.
The UNIX file system defines 3 fields of 3 bits each: rwx
r( read
access)
w(write
access)
x(execute
access)
Separate fields are kept for file owners, group & other users. So, a bit is
needed to record protection
information for each file.
Allocation methods
There are 3 methods of allocating disk space widely used.
1. Contiguous allocation:
a. It requires each file to occupy a set of contiguous blocks on the disk.
b. Number of disk seeks required for accessing contiguously allocated
file is minimum.
c. The IBM VM/CMS OS uses contiguous allocation. Contiguous
allocation of a file is defined by the disk address and length (in terms
of block units).
d. If the file is ‘n’ blocks long and starts all location ‘b’, then it occupies
blocks b, b+1, b+2,----
-------- - -b+ n-1.
e. The directory for each file indicates the address of the starting
block and the length of the area allocated for each file.
f. Contiguous allocation supports both sequential and direct access. For
sequential access, the
file system remembers the disk address of the last block referenced
and reads the next block when necessary.
g. For direct access to block i of a file that starts at block b we can
immediately access block b
+ i.
h. Problems: One difficulty with contiguous allocation is finding
space for a new file. It also suffers from the problem of external
fragmentation. As files are deleted and allocated, the
free disk space is broken into small pieces. A major problem in
contiguous allocation is how

83 | P a g e
much space is needed for a file. When a file is created, the total
amount of space it will need must be found and allocated. Even if the
total amount of space needed for a file is known in advances, pre-
allocation is inefficient. Because a file that grows very slowly must be
allocated enough space for its final size even though most of that
space is left unused for a long
period time. Therefore, the file has a large amount of
internal fragmentation.
2. Linked Allocation:
a. Linked allocation solves all problems of contiguous allocation.
b. In linked allocation, each file is linked list of disk blocks, which are
scattered throughout the disk.
c. The directory contains a pointer to the first and last blocks of the file.
d. Each block contains a pointer to the next block.
e. These pointers are not accessible to the user. To create a new file,
we simply create a new entry in the directory.
f. For writing to the file, a free block is found by the free space
management system and this
new block is written to & linked to the
end of the file.
g. To read a file, we read blocks by following the pointers from block to
block.
h. There is no external fragmentation with linked allocation & any
free block can be used to satisfy a request.
i. Also there is no need to declare the size of a file when that file is
created. A file can continue to grow as long as there are free blocks.
j. Limitations: It can be used effectively only for sequential access
files. To find the ‘ i ' th block of the file, we must start at the
beginning of that file and follow the pointers until we get the ith
block. So it is inefficient to support direct access files. Due to the
presence of pointers each file requires slightly more space than
before. Another problem is reliability. Since the files are linked
together by pointers scattered throughout the disk. What would
happen if a pointer were lost or damaged.
3. Indexed Allocation:
a. Indexed allocation solves the problem of linked allocation by
bringing all the pointers together to one location known as the
index block.
b. Each file has its own index block which is an array of disk block
addresses. The ith entry in
the index block points to the ith block
of the file.

84 | P a g e
c. The directory contains the address of the index block. The read
the ith block, we use the pointer in the ith index block entry and
read the desired block.
d. To write into the ith block, a free block is obtained from the free
space manager and its address is put in the ith index block entry.
e. Indexed allocation supports direct access without suffering external
fragmentation.
f. Limitations: The pointer overhead of index block is greater than the
pointer overhead of linked allocation. So here more space is wasted
than linked allocation. In indexed allocation, an entire index block
must be allocated, even if most of the pointers are nil.
Free Space Management
Since there is only a limited amount of disk space, it is necessary to reuse
the space from the deleted files. To keep track of free disk space, the system
maintains a free space list. It records all the disk blocks that are free i.e. not
allocated to some file or dictionary. To create a file, we search the free space
list for the required amount of space and allocate it to the new file. This
space is then removed from the free space list. When a file is deleted, its
disk space is added to the free space list. Implementation:
There are 4 ways to implement the free space list such as:
Bit Vector: The free space list is implemented as a bit map or bit
vector. Each block is represented as 1 bit. If the block is free, the bit is 1
and if it is allocated then the bit is 0. For example, consider a disk
where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 26 & 27 are free
and rest of the blocks are allocated. The free space bit map would be
0011110011111100011000000111……………………..
The main advantage of this approach is that it is simple and efficient to
find the first free block or n consecutive free blocks on the disk. But bit
vectors are inefficient unless the entire vector is kept in main memory. It
is possible for smaller disks but not for larger ones.
Linked List: Another approach is to link together all the free disk blocks
and keep a pointer to the first free block. The first free block contains a
pointer to the next free block and so on. For example, we keep a pointer
to block 2 as the free block. Block 2 contains a pointer to block which
points to block 4 which then points to block 5 and so on. But this scheme
is not efficient.
To traverse the list, we must read each block which require a lot of I/O
time.

85 | P a g e
Grouping: In this approach, we store the address of n free blocks in the
first free block. The first n-1 of these blocks is actually free. The last
block contains the address of another n free blocks and so on. Here the
addresses of a large number of free blocks can be found out quickly.
Counting: Rather than keeping a list of n free disk block addresses, we
can keep the address of
the first free block and the number of free contiguous blocks. So here
each entry in the free
space list consists of a disk address and a count.

86 | P a g e

You might also like