0% found this document useful (0 votes)
28 views33 pages

Unit 4-3

Uploaded by

dsptutorials77
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)
28 views33 pages

Unit 4-3

Uploaded by

dsptutorials77
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/ 33

UNIT -4

VIRTUAL MEMORY
What is Virtual Memory in OS (Operating System)?
Virtual memory in an operating system is a memory
management technique that creates an illusion of a large block
of contiguous memory for users. It uses both physical
memory (RAM) and disk storage to provide a larger virtual
memory space, allowing systems to run larger applications and
handle more processes simultaneously. This helps
improve system performance and multitasking efficiency.

How Virtual Memory Works?


In modern word, virtual memory has become quite common
these days. In this scheme, whenever some pages needs to be
loaded in the main memory for the execution and the memory
is not available for those many pages, then in that case, instead
of stopping the pages from entering in the main memory, the
OS search for the RAM area that are least used in the recent
times or that are not referenced and copy that into the
secondary memory to make the space for the new pages in the
main memory.
Since all this procedure happens automatically, therefore it
makes the computer feel like it is having the unlimited RAM.

Advantages of Virtual Memory


1. The degree of Multiprogramming will be increased.
2. User can run large application with less real RAM.
3. There is no need to buy more memory RAMs.
Disadvantages of Virtual Memory
1. The system becomes slower since swapping takes time.
2. It takes more time in switching between applications.
3. The user will have the lesser hard disk space for its use.
Types of Virtual memory
The two types of virtual memory in an operating system (OS)
are paging and segmentation:
 Paging: Divides memory into fixed-sized blocks, or pages,
for efficient storage.
 Segmentation: Divides memory into variable-sized
segments, which allows for more flexibility in memory
management.
In modern computer systems, a technique called segmented
paging is often used to combine the benefits of both paging and
segmentation.
Virtual memory allows programs to access memory without
needing to be concerned with physical memory constraints. It
also helps the OS allocate memory resources more efficiently.
Demand Paging
What is Demand Paging in OS?
The demand paging system is similar to the swapping paging
system in that processes are mostly stored in the main memory
(usually on the hard disk). As a result, demand paging is a
procedure that addresses the problem above just by shifting
pages on demand. Lazy swapper is another name for this ( It
never swaps the page into the memory unless it is needed).
A pager is a kind of swapper that deals with the individual
pages of a process.
Demand Paging is a method in which a page is only brought
into main memory when the CPU requests it. At first, just those
pages are loaded directly required by the operation. Pages that
are never accessed are thus never loaded into physical memory.

Common Terms in Demand Paging Operating System


Following are some common terms in demand paging
operating systems:
 Page Fault
 Swapping
 Thrashing
1.Page Fault
There will be a miss if the referenced page is not present in the
main memory; this is known as a page miss or page fault.
The CPU must look up the missing page in secondary memory.
When the number of page faults is significant, the system's
effective access time increases dramatically.
2. Swapping
Swapping comprises either erasing all of the process's pages
from memory or marking the pages so that we can remove
them via the page replacement method.
When a process is suspended, it indicates it is unable to run.
However, we can change the process for a while. The system
can swap the process from secondary memory to primary
memory over a period of time. Thrashing describes a condition
in which a process is busy, and the pages are swapped in and
out of it.
3. Thrashing
The effective access time will be the time needed by the CPU to
read one word from the secondary memory if the number of
page faults is equal to the number of referred pages or if the
number of page faults is so high that the CPU is only reading
pages from the secondary memory. This is known as Thrashing.
Pure Demand Paging
Pure demand paging is a specific implementation of demand
paging. The operating system only loads pages into memory
when the program needs them. In on-demand paging only, no
pages are initially loaded into memory when the program
starts, and all pages are initially marked as being on disk.
Operating systems that use pure demand paging as a memory
management strategy do so without preloading any pages into
physical memory prior to the commencement of a task.
Demand paging loads a process’s whole address space into
memory one step at a time, bringing just the parts of the
process that are actively being used into memory from disc as
needed.
It is useful for executing huge programs that might not fit
totally in memory or for computers with limited physical
memory. If the program accesses a lot of pages that are not in
memory right now, it could also result in a rise in page faults
and possible performance overhead. Operating systems
frequently use caching techniques and improve page
replacement algorithms to lessen the negative effects of page
faults on system performance as a whole.
Working Process of Demand Paging
Let us understand this with the help of an example. Suppose
we want to run a process P which have four pages P0, P1, P2,
and P3. Currently, in the page table, we have pages P1 and P3.
Steps of Demand Paging
Whenever a page request comes, the table is consulted first.
Some frames have invalid bit means either they are not in the
main memory then-
 In case of a page fault, an interrupt is generated.
 After interrupting the current process, the operating
system blocks the process and moves to the blocked state.
 The requested process is searched in the secondary
memory.
 Check for the free frame, but page replacement algorithms
are followed if you do not find any free frame.
 The page is replaced(from secondary memory to main
memory) with the help of the page replacement
algorithms.
 The CPU is informed about that update. And asked to
proceed with the execution, and the process returns to its
ready state. The page table is updated accordingly.
Advantages of Demand Paging
 It can improve performance by allowing the operating
system to keep more programs and files in memory,
thereby reducing the number of times that they need to be
loaded from the disk.
 It can allow the operating system to use more memory
than is physically installed by using virtual memory.
 Fast response times to requests,
 Greater throughput (the ability to service more requests in
the same amount of time)
Disadvantages of Demand Paging
 Can slow down the system because it requires the kernel
to keep track of which pages are in use by the applications
and which are free.
 Lead to memory address space fragmentation, making it
harder for the system to process applications.
 Causes the system to page in applications from disk too
frequently, leading to data corruption.
 Memory access time is longer.
 Overhead increases due to page table access and interrupt
handling.
Page Replacement Algorithms
Page replacement algorithms are techniques used
in operating systems to manage memory efficiently when
the virtual memory is full. When a new page needs to be
loaded into physical memory , and there is no free space,
these algorithms determine which existing page to
replace.
If no page frame is free, the virtual memory manager
performs a page replacement operation to replace one of
the pages existing in memory with the page whose
reference caused the page fault. It is performed as follows:
The virtual memory manager uses a page replacement
algorithm to select one of the pages currently in memory
for replacement, accesses the page table entry of the
selected page to mark it as “not present” in memory, and
initiates a page-out operation for it if the modified bit of
its page table entry indicates that it is a dirty page.
Common Page Replacement Techniques
 First In First Out (FIFO)
 Optimal Page replacement
 Least Recently Used
 Most Recently Used (MRU)

File system
What is a file system?
A file system is a method an operating system uses
to store, organize, and manage files and directories
on a storage device. Some common types of file
systems include:
 FAT (File Allocation Table): An older file
system used by older versions of Windows and
other operating systems.
 NTFS (New Technology File System): A
modern file system used by Windows. It
supports features such as file and folder
permissions, compression, and encryption.
 ext (Extended File System): A file system
commonly used on Linux and Unix-based
operating systems.
 HFS (Hierarchical File System): A file system
used by macOS.
 APFS (Apple File System): A new file system
introduced by Apple for their Macs and iOS
devices.
A file is a collection of related information that is
recorded on secondary storage. Or file is a
collection of logically related entities. From the
user’s perspective, a file is the smallest allotment of
logical secondary storage.
The name of the file is divided into two parts as
shown below:
Name
Extension, separated by a period.
File Attributes
File attributes are configuration and information
related to files. These attributes grant/deny requests
of a user/process for access, modifying, relocating,
or deleting it.
 Name- This is the only information stored in a
human-readable format. A name is given to
each file. One directory cannot contain two files
with the same name.
 Extension- This attribute is required for
systems that support different file types. For
example, a text file has a .txt extension and a
video file has a .mp4 extension.
 Location- Refers to the location of the file on
the device. The file system has several places
where you can save your files. Each file
contains its location as an attribute.
 Size-File- size means the number of bytes
captured by the file in memory.
 Identifier- Each file is identified by a unique
tag number within the file system called an
identifier.
 Protection- This attribute assigns and controls
access to read, write, and execute files.
Computer administrators may need different
protection for each file. Therefore, each file has
its own permissions on different user groups.
 Compression- It tells the type of compression
there is.
 Created/Modified date- Each file has a
timestamp that includes the date and time the
file was last modified.
File Operations
 Creating- Creating a file involves two steps. First,
check if there is free space. You must create a new
directory file entry if you have enough space.
 Opening- This is a common operation performed
on a file. After you create a file, you need to open it
before you can perform any file processing
operations. If the user wants to open the file, call a
system call to instruct the operating system to open
the file and pass the file name to the file
 Writing- To write a file, you need to know two
things. One is the name of the file and the other is
the information or data written to the file
 Deleting- To delete a file, first browse the directory
of the named file, then free up the file area and
delete the directory entry.
 Truncating- To truncate a file, delete only the
contents of the file, but don’t delete its file structure.
 Close File- After editing the file, you need to close
the file to free up any permanent changes made and
occupied resources. Finally, the internal descriptor
created when the file was opened is released.
File Types
File Structure
A file is a logical unit of information. They are produced by
processes. The operating system manages files. While creating
a file, a name is assigned to it. After this process has
terminated, the file exists and remains accessible to other
processes. The name of a file has two parts, separated by a dot.
Example, code.cpp is a c++ program file with name code and
extension cpp. The name of the file before the dot is a label for
the file's identification, while the part after the dot is called the
file extension which indicates the type of the file.
File Access Methods
There are three ways to access a file in a computer system:
Sequential-Access
Direct Access
Index sequential Method
Sequential-Access
It is the simplest access method. Information in the file is
processed in order, one record after the other. This mode of
access is by far the most common; for example, the editor and
compiler usually access the file in this fashion.
Read and write make up the bulk of the operation on a file. A
read operation -read next- reads the next position of the file
and automatically advances a file pointer, which keeps track of
the I/O location. Similarly, for the -write next- append to the
end of the file and advance to the newly written material.

Advantages of Sequential Access:


 The sequential access mechanism is very easy to
implement.
 It uses lexicographic order to enable quick access to the
next entry.
Disadvantages of Sequential Access:
 Sequential access will become slow if the next file record
to be retrieved is not present next to the currently pointed
record.
 Adding a new record may need relocating a significant
number of records of the file.
Direct Access
Another method is direct access method also known as relative
access method. A fixed-length logical record that allows the
program to read and write record rapidly in no particular
order. The direct access is based on the disk model of a file
since disk allows random access to any file block. For direct
access, the file is viewed as a numbered sequence of block or
record. Thus, we may read block 14 then block 59, and then we
can write block 17. There is no restriction on the order of
reading and writing for a direct access file.
A block number provided by the user to the operating system is
normally a relative block number, the first relative block of the
file is 0 and then 1 and so on.
.
Advantages of Direct/Relative Access:
 The files can be retrieved right away with a direct access
mechanism, reducing the average access time of a file.
 There is no need to traverse all of the blocks that come
before the required block to access the record.
Disadvantages of Direct/Relative Access:
 The direct access mechanism is typically difficult to
implement due to its complexity.
 Organizations can face security issues as a result of direct
access as the users may access/modify the sensitive
information. As a result, additional security processes
must be put in place.
3. Indexed Sequential Access
It's the other approach to accessing a file that's constructed on
top of the sequential access mechanism. This method is
practically similar to the pointer-to-pointer concept in which
we store the address of a pointer variable containing the
address of some other variable/record in another pointer
variable. The indexes, similar to a book's index (pointers),
contain a link to various blocks present in the memory. To
locate a record in the file, we first search the indexes and then
use the pointer-to-pointer concept to navigate to the required
file.
Primary index blocks contain the links of the secondary inner
blocks which contain links to the data in the memory.

Advantages of Indexed Sequential Access:


 If the index table is appropriately arranged, it accesses the
records very quickly.
 Records can be added at any position in the file quickly.
Disadvantages of Indexed Sequential Access:
 When compared to other file access methods, it is costly
and less efficient.
 It needs additional storage space.
https://www.rcet.org.in/uploads/academics/rohini_92015599388
.pdf
DIRECTORY AND DISK STRUCTURE
What is the Directory Structure in OS?
A directory is a container that stores files and folders,
organizing them hierarchically. The Directory Structure in OS
manages entries of files, including file names, locations,
protection info, and more. This structure enables efficient file
retrieval.

Operations in Directory Structure in OS


Creating: Users can create new files and directories, providing
unique names for directories.
Searching: Users can search for specific files or directories
within a directory.
Deleting: Unwanted files or empty directories can be deleted.
Listing: Users can retrieve a list of files in a directory.
Renaming: Files and directories can be renamed to reflect
content changes.
Linking: Files can be linked to appear in multiple directories.
Unlinking: Removing links from files in multiple directories.
There are five directory structures. They are
1. Single-level directory
2. Two-level directory
3. Tree-Structured directory
4. Acyclic Graph directory
5. General Graph directory
1. Single – Level Directory
• The simplest directory structure is the single- level directory.
• All files are contained in the same directory.

Advantages
 Implementation is very simple.
 If the sizes of the files are very small then the searching
becomes faster.
 File creation, searching, deletion is very simple since we
have only one directory.
Disadvantages
 We cannot have two files with the same name.
 The directory may be very big therefore searching for a
file may take so much time.
 Protection cannot be implemented for multiple users.
 There are no ways to group same kind of files.
 Choosing the unique name for every file is a bit complex
and limits the number of files in the system because most
of the Operating System limits the number of characters
used to construct the file name.
2. Two – Level Directory
In two level directory systems, we can create a separate
directory for each user. There is one master directory which
contains separate directories dedicated to each user. For each
user, there is a different directory present at the second level,
containing group of user's file. The system doesn't let a user to
enter in the other user's directory without permission.
Advantages
The main advantage is there can be more than two files with
same name, and would be very helpful if there are multiple
users.
A security would be there which would prevent user to access
other user’s files.
Searching of the files becomes very easy in this directory
structure.
Disadvantage:
• Users cannot create their own sub-directories.

3. Tree – Structured Directory


• A tree is the most common directory structure.
• The tree has a root directory. Every file in the system has a
unique path name.
• A path name is the path from the root, through all the
subdirectories to a specified
file.
• A directory (or sub directory) contains a set of files or sub
directories.
• One bit in each directory entry defines the entry as a file (0)
or as a subdirectory (1).
• Special system calls are used to create and delete directories.
• Path names can be of two types: absolute path names or
relative path names.
• An absolute path name begins at the root and follows a path
down to the specified
file, giving the directory names on the path.
• A relative path name defines a path from the current
directory.

Advantages:

Scalability: The tree structure is highly scalable, as directories


can contain other directories, allowing for efficient organization
of large numbers of files.
Reduced Naming Conflicts: File name collisions are less likely
because files can be placed in different directories, which
makes it easier to have files with the same name but different
paths.
Efficient Searching: Searching is more straightforward and
efficient because users can specify either an absolute path
(starting from the root directory) or a relative path (from the
current working directory).
Disadvantages:
Rigid Hierarchical Structure: Some files may not fit neatly into
the hierarchical structure, making it difficult to decide which
directory they belong to.
Complexity in File Sharing: By default, files in a tree structure
are organized hierarchically, which can make file sharing more
complicated. However, this can be resolved by using
mechanisms like links (symbolic or hard links) to share files
between directories.
Duplication through Links: While files themselves are not
duplicated, links can point to the same file in multiple
directories. This can cause confusion but does not result in
actual duplication of the file.
4. Acyclic Graph Directory.
An acyclic graph is a graph with no cycles.
To implement shared files and subdirectories this directory
structure is used.
An acyclic – graph directory structure is more flexible than is
a simple tree structure,
but it is also more complex.
Implementations of shared files or directories
1. Links
– A new type of directory entry is created. It is effectively a
pointer to another
file or subdirectory
– Links are implemented as an absolute or relative path name.
– The deletion of a link does not need to affect the original file;
only the link is
removed.
2. Duplicate all information in sharing directories
Problems to consider with link implementation:
• Upon traversal of file system, do not want to traverse shared
structures more than
Once on deletion.
Advantages:
File Sharing: The acyclic-graph structure allows files to be
shared between directories, enabling more flexible file
organization.
Multiple Access Paths: Since the same file can have multiple
paths leading to it, it simplifies searching and access in some
scenarios.
Disadvantages:
Complexity in File Removal: When using links, deleting files
can become more complex. For symbolic links, deletion of the
original file leaves dangling pointers. For hard links, the system
must ensure that all references are removed before the file is
deleted.
Dangling Symbolic Links: If a file is deleted, symbolic links
pointing to it remain but are non-functional, which can cause
confusion.
Reference Management with Hard Links: With hard links, the
file's data remains on disk until all references (hard links) to it
are deleted. This can make file management and removal more
challenging.
General-Graph Directory Structure In OS
Cycles are allowed inside a directory structure where
numerous directories can be derived from more than one
parent directory in a general graph directory structure. When
general graph directories are allowed, commands like, search a
directory and its subdirectories, must be used with caution. If
cycles are allowed, the search is infinite.

The biggest issue with this type of directory layout is figuring


out how much space the files and folders have used up.
Advantages:
Flexibility: The general-graph structure is more flexible than
other directory structures because files and directories can have
multiple parent directories, making it adaptable for complex
organizational needs.
Cycles Allowed: Cycles are permitted, allowing for more
intricate and flexible linking of directories.
Disadvantages:
Higher Cost: It is more computationally expensive to manage,
as more complex algorithms are needed to handle tasks like
searching, cycle detection, and garbage collection.
Garbage Collection: A general-graph structure requires garbage
collection to ensure that files are not orphaned or left
inaccessible after their parent directories are removed.
Space Accounting: Determining how much space a file or
directory uses can be challenging, as a file may have multiple
references, complicating storage management.
File Allocation Methods
The allocation methods define how the files are stored in the
disk blocks. There are three main disk space or file allocation
methods.

Contiguous Allocation
Linked Allocation
Indexed Allocation
The main idea behind these methods is to provide:
Efficient disk space utilization.
Fast access to the file blocks.
All the three methods have their own advantages and
disadvantages as discussed below:
1. Contiguous Allocation
In this scheme, each file occupies a contiguous set of blocks on
the disk. For example, if a file requires n blocks and is given a
block b as the starting location, then the blocks assigned to the
file will be: b, b+1, b+2,……b+n-1. This means that given the
starting block address and the length of the file (in terms of
blocks required), we can determine the blocks occupied by the
file.
The directory entry for a file with contiguous allocation
contains
Address of starting block
Length of the allocated portion.
The file ‘mail’ in the following figure starts from the block 19
with length = 6 blocks. Therefore, it occupies 19, 20, 21, 22, 23,
24 blocks.
Advantages:
Both the Sequential and Direct Accesses are supported by this.
For direct access, the address of the kth block of the file which
starts at block b can easily be obtained as (b+k).
This is extremely fast since the number of seeks are minimal
because of contiguous allocation of file blocks.
Disadvantages:
This method suffers from both internal and external
fragmentation. This makes it inefficient in terms of memory
utilization.
Increasing file size is difficult because it depends on the
availability of contiguous memory at a particular instance.
2. Linked List Allocation
In this scheme, each file is a linked list of disk blocks which
need not be contiguous. The disk blocks can be scattered
anywhere on the disk.
The directory entry contains a pointer to the starting and the
ending file block. Each block contains a pointer to the next
block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are
randomly distributed. The last block (25) contains -1 indicating
a null pointer and does not point to any other block.
Advantages:

This is very flexible in terms of file size. File size can be


increased easily since the system does not have to look for a
contiguous chunk of memory.
This method does not suffer from external fragmentation. This
makes it relatively better in terms of memory utilization.
Disadvantages:
Because the file blocks are distributed randomly on the disk, a
large number of seeks are needed to access every block
individually. This makes linked allocation slower.
It does not support random or direct access. We can not directly
access the blocks of a file. A block k of a file can be accessed by
traversing k blocks sequentially (sequential access ) from the
starting block of the file via block pointers.
Pointers required in the linked allocation incur some extra
overhead.
3. Indexed Allocation
In this scheme, a special block known as the Index block
contains the pointers to all the blocks occupied by a file. Each
file has its own index block. The ith entry in the index block
contains the disk address of the ith file block. The directory
entry contains the address of the index block as shown in the
image:

Advantages:
This supports direct access to the blocks occupied by the file
and therefore provides fast access to the file blocks.
It overcomes the problem of external fragmentation.
Disadvantages:
The pointer overhead for indexed allocation is greater than
linked allocation.
For very small files, say files that expand only 2-3 blocks, the
indexed allocation would keep one entire block (index block)
for the pointers which is inefficient in terms of memory
utilization. However, in linked allocation we lose the space of
only 1 pointer per block.
File system structure

You might also like