FYIT OS Unit 3
FYIT OS Unit 3
Swapping has been subdivided into two concepts: swap-in and swap-
out.
● Swap-out is a technique for moving a process from RAM to the
hard disc.
● Swap-in is a method of transferring a program from a hard disc
to main memory, or RAM.
Process of Swapping
● When the RAM is full and a new program needs to run, the
operating system selects a program or data that is currently in
RAM but not actively being used.
● The selected data is moved to secondary storage, freeing up
space in RAM for the new program
● When the swapped-out program is needed again, it can be
swapped back into RAM, replacing another inactive program or
data if necessary.
Real Life Example of Swapping
Imagine you have a disk (RAM) that is too small to hold all your
books and papers (programs). You keep the most important items on
the desk and store the rest in a cabinet (secondary storage). When you
need something from the cabinet, you swap it with something on your
desk. This way, you can work with more items than your desk alone
could hold.
Memory allocation
Memory allocation in an operating system refers to the process of
assigning memory spaces to programs or processes during their
execution. The operating system must efficiently allocate and manage
memory to ensure that processes get the memory they need without
wasting resources or interfering with each other.
Types of Memory Allocation:
Memory fragmentation
Memory fragmentation in an operating system refers to the
phenomenon where free memory is split into small, non-contiguous
blocks over time, which can lead to inefficient memory utilization.
Fragmentation occurs when memory is allocated and deallocated
dynamically, leaving gaps of unused memory that are not large
enough to satisfy new memory allocation requests.
There are two main types of fragmentation:
1. External Fragmentation:
Non-Contiguous Allocation
Translation
The address generated by the CPU is divided into:
● Segment number (s): Number of bits required to represent
the segment.
● Segment offset (d): Number of bits required to represent the
position of data within a segment.
Advantages of Segmentation in Operating System
● Reduced Internal Fragmentation : Segmentation can reduce
internal fragmentation compared to fixed-size paging, as
segments can be sized according to the actual needs of a
process. However, internal fragmentation can still occur if a
segment is allocated more space than it is actually used.
● Segment Table consumes less space in comparison to Page
table in paging.
● As a complete module is loaded all at once, segmentation
improves CPU utilization.
● The user’s perception of physical memory is quite similar to
segmentation. Users can divide user programs into modules
via segmentation. These modules are nothing more than
separate processes’ codes.
● The user specifies the segment size, whereas, in paging, the
hardware determines the page size.
● Segmentation is a method that can be used to segregate data
from security operations.
● Flexibility: Segmentation provides a higher degree of
flexibility than paging. Segments can be of variable size, and
processes can be designed to have multiple segments,
allowing for more fine-grained memory allocation.
● Sharing: Segmentation allows for sharing of memory
segments between processes. This can be useful for inter-
process communication or for sharing code libraries.
● Protection: Segmentation provides a level of protection
between segments, preventing one process from accessing or
modifying another process’s memory segment. This can
help increase the security and stability of the system.
Disadvantages of Segmentation in Operating System
● External Fragmentation : As processes are loaded and
removed from memory, the free memory space is broken
into little pieces, causing external fragmentation. This is a
notable difference from paging, where external
fragmentation is significantly lesser.
● Overhead is associated with keeping a segment table for
each activity.
● Due to the need for two memory accesses, one for the
segment table and the other for main memory, access time to
retrieve the instruction increases.
● Fragmentation: As mentioned, segmentation can lead to
external fragmentation as memory becomes divided into
smaller segments. This can lead to wasted memory and
decreased performance.
● Overhead: Using a segment table can increase overhead and
reduce performance. Each segment table entry requires
additional memory, and accessing the table to retrieve
memory locations can increase the time needed for memory
operations.
● Complexity: Segmentation can be more complex to
implement and manage than paging. In particular, managing
multiple segments per process can be challenging, and the
potential for segmentation faults can increase as a result.
Thrashing
Thrashing is a condition or a situation when the system is
spending a major portion of its time servicing the page faults,
but the actual processing done is very negligible.
Causes of thrashing:
1. High degree of multiprogramming.
2. Lack of frames.
3. Page replacement policy.
Thrashing’s Causes
Thrashing has an impact on the operating system’s execution
performance. Thrashing also causes serious performance issues
with the operating system. When the CPU’s usage is low, the
process scheduling mechanism tries to load multiple processes
into memory at the same time, increasing the degree of Multi
programming.
In this case, the number of processes in the memory exceeds the
number of frames available in the memory. Each process is given
a set number of frames to work with.
Thrashing occurs when a system spends more time swapping
pages in and out of memory than executing processes, leading to a
significant drop in performance. Learning techniques to handle
thrashing is crucial for optimizing system performance.
If a high-priority process arrives in memory and the frame is not
vacant at the moment, the other process occupying the frame will
be moved to secondary storage, and the free frame will be allotted
to a higher-priority process.
We may also argue that as soon as the memory is full, the
procedure begins to take a long time to swap in the required
pages. Because most of the processes are waiting for pages, the
CPU utilization drops again.
As a result, a high level of multiprogramming and a lack of
frames are two of the most common reasons for thrashing in the
operating system.
The basic concept involved is that if a process is allocated to few
frames, then there will be too many and too frequent page faults.
As a result, no useful work would be done by the CPU and the
CPU utilization would fall drastically. The long-term scheduler
would then try to improve the CPU utilization by loading some
more processes into the memory thereby increasing the degree of
multi programming. This would result in a further decrease in the
CPU utilization triggering a chained reaction of higher page faults
followed by an increase in the degree of multi programming,
called Thrashing.
What is Kernel Memory Allocation?
In operating system design, the kernel memory allocation is a
critical aspect which involves the allocation of memory for kernel
level operations and data structures. When a process is executing
in user mode and it requests the additional memory, then the
kernel maintains the allocation of pages from the list of free page
frames.
Storage Management
File Systems
A computer file is defined as a medium used for saving and managing data in the computer
system. The data stored in the computer system is completely in digital format, although there
can be various types of files that help us to store the data.
File systems are a crucial part of any operating system, providing a structured way to store,
organize, and manage data on storage devices such as hard drives, SSDs, and USB drives.
Essentially, a file system acts as a bridge between the operating system and the physical
storage hardware, allowing users and applications to create, read, update, and delete files in an
organized and efficient manner.
● FAT (File Allocation Table): An older file system used by older versions of Windows and
other operating systems.
4. NTFS (New Technology File System): A modern file system used by Windows. It
supports features such as file and folder permissions, compression, and encryption.
3. ext (Extended File System): A file system commonly used on Linux and Unix-based
operating systems.
● 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
File access methods in OS are nothing but techniques to read data from the system's memory.
There are various ways in which we can access the files from the memory like:
● Sequential Access
● Direct/Relative Access, and
● Indexed Sequential Access.
These methods by which the records in a file can be accessed are referred to as the file access
mechanism. Each file access mechanism has its own set of benefits and drawbacks, which are
discussed further in this article.
● 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.
● 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.
● 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.
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.
File Sharing:
File Sharing in an Operating System(OS) denotes how information and files are shared between
different users, computers, or devices on a network; and files are units of data that are stored in
a computer in the form of documents/images/videos or any others types of information
needed.
For Example: Suppose letting your computer talk to another computer and exchange pictures,
documents, or any useful data. This is generally useful when one wants to work on a project
with others, send files to friends, or simply shift stuff to another device. Our OS provides ways
to do this like email attachments, cloud services, etc. to make the sharing process easier and
more secure.
Now, file sharing is nothing like a magical bridge between Computer A to Computer B allowing
them to swap some files with each other.
Let's see the various ways through which we can achieve file sharing in an OS.
SMB is like a network based file sharing protocol mainly used in windows operating systems. It
allows our computer to share files/printer on a network. SMB is now the standard way for
seamless file transfer method and printer sharing.
Example: Imagine in a company where the employees have to share the files on a particular
project . Here SMB is employed to share files among all the windows based operating
[Link] on projects. SMB/CIFS is employed to share files between Windows-based
computers. Users can access shared folders on a server, create, modify, and delete files.
NFS is a distributed based file sharing protocol mainly used in Linux/Unix based operating
System. It allows a computer to share files over a network as if they were based on local. It
provides a efficient way of transfer of files between servers and clients.
It is the most common standard protocol for transferring of the files between a client and a
server on a computer network. FTPs supports both uploading and downloading of the files,
here we can download,upload and transfer of files from Computer A to Computer B over the
internet or between computer systems.
Example: Suppose the developer makes changes on the server. Using the FTP protocol, the
developer connects to the server they can update the server with new website content and
updates the existing file over there.
It involves the famous ways of using online services like Google Drive, DropBox , One
Drive ,etc. Any user can store files over these cloud services and they can share that with
others, and providing access from many users. It includes collaboration in realtime file sharing
and version control access.
Ex: Several students working on a project and they can use Google Drive to store and share for
that purpose. They can access the files from any computer or mobile devices and they can
make changes in realtime and track the changes over there.
These all file sharing methods serves different purpose and needs according to the
requirements and flexibility of the users based on the operating system.
File system implementation is a critical aspect of an operating system as it directly impacts the
performance, reliability, and security of the system. Different operating systems use different
file system implementations based on the specific needs of the system and the intended use
cases. Some common file systems used in operating systems include NTFS and FAT in
Windows, and ext4 and XFS in Linux.
Components of File System Implementation
● File System Structure: The file system structure refers to how the files and directories
are organized and stored on the physical storage device. This includes the layout of file
systems data structures such as the directory structure, file allocation table, and inodes.
● File Allocation: The file allocation mechanism determines how files are allocated on the
storage device. This can include allocation techniques such as contiguous allocation,
linked allocation, indexed allocation, or a combination of these techniques.
● Data Retrieval: The file system implementation determines how the data is read from
and written to the physical storage device. This includes strategies such as buffering
and caching to optimize file I/O performance.
● Security and Permissions: The file system implementation includes features for
managing file security and permissions. This includes access control lists (ACLs), file
permissions, and ownership management.
● Recovery and Fault Tolerance: The file system implementation includes features for
recovering from system failures and maintaining data integrity. This includes techniques
such as journaling and file system snapshots.
Implementation Issues
● Management of Disc space: To prevent space wastage and to guarantee that files can
always be stored in contiguous blocks, file systems must manage disc space effectively.
Free space management, fragmentation prevention, and garbage collection are
methods for managing disc space.
● Checking for Consistency and Repairing Errors: The consistency and error-free
operation of files and directories must be guaranteed by file systems. Journaling,
checksumming, and redundancy are methods for consistency checking and error
recovery. File systems may need to perform recovery operations if errors happen in
order to restore lost or damaged data.
● Locking Files and Managing Concurrency: To prevent conflicts and guarantee data
integrity, file systems must control how many processes or users can access a file at
once. File locking, semaphore, and other concurrency-controlling methods are
available.
● Performance Optimization: File systems need to optimize performance by reducing file
access times, increasing throughput, and minimizing system overhead. Caching,
buffering, prefetching, and parallel processing are methods for improving performance.
● Partitioning The Storage Device: The first step in file system implementation is to
partition the physical storage device into one or more logical partitions. Each partition is
formatted with a specific file system that defines the way files and directories are
organized and stored.
● File System Structures: File system structures are the data structures used by the
operating system to manage files and directories. Some of the key file system structures
include the superblock, inode table, directory structure, and file allocation table.
● Allocation of Storage Space: The file system must allocate storage space for each file
and directory on the storage device. There are several methods for allocating storage
space, including contiguous, linked, and indexed allocation.
● File Operations: The file system provides a set of operations that can be performed on
files and directories, including create, delete, read, write, open, close, and seek. These
operations are implemented using the file system structures and the storage allocation
methods.
● File System Security: The file system must provide security mechanisms to protect files
and directories from unauthorized access or modification. This can be done by setting
file permissions, access control lists, or encryption.
● File System Maintenance: The file system must be maintained to ensure efficient and
reliable operation. This includes tasks such as disk defragmentation, disk checking, and
backup and recovery.
When a file is created, the operating system searches the free space list for the required space
allocated to save a file. While deletion a file, the file system frees the given space and adds
this to the free space list.
A bit vector is a most frequently used method to implement the free space list. A bit vector is
also known as a Bit map. It is a series or collection of bits in which each bit represents a disk
block. The values taken by the bits are either 1 or 0. If the block bit is 1, it means the block is
empty and if the block bit is 0, it means the block is not free. It is allocated to some files.
Since all the blocks are empty initially so, each bit in the bit vector represents 0.
● The operating system goes through all the blocks until it finds a free block. (block
whose bit is '0').
● It is not efficient when the disk size is large.
Linked List
A linked list is another approach for free space management in an operating system. In it, all
the free blocks inside a disk are linked together in a linked list. These free blocks on the disk
are linked together by a pointer. These pointers of the free block contain the address of the
next free block and the last pointer of the list points to null which indicates the end of the
linked list. This technique is not enough to traverse the list because we have to read each disk
block one by one which requires I/O time.
In the above example, we have three blocks of free memory, each represented by a node in
the linked list. The first block has 20 bytes of free memory, the second block has 20 bytes of
free memory, and the third block has 60 bytes of free memory. The operating system can use
this linked list to allocate memory blocks to processes as needed.
Disadvantages
Grouping
The grouping technique is also called the "modification of a linked list technique". In this
method, first, the free block of memory contains the addresses of the n-free blocks. And the
last free block of these n free blocks contains the addresses of the next n free block of
memory and this keeps going on. This technique separates the empty and occupied blocks of
space of memory.
Example
Suppose we have a disk with some free blocks and some occupied blocks. The free block
numbers are 3,4,5,6,9,10,11,12,13,3,4,5,6,9,10,11,12,13,, and 1414. And occupied
block numbers are 1,2,7,8,15,1,2,7,8,15, and 1616 i.e.i.e., they are allocated to some
files.
When the "grouping technique" is applied, block 3 will store the addresses of blocks 4, 5, and
6 because block 3 is the first free block. In the same way, block 6 will store the addresses of
blocks 9, one 0, and one 1 because block 6 is the first occupied block.
1. By using this method, we can easily find addresses of a large number of free blocks
easily and quickly.
Disadvantage
Counting
In memory space, several files are created and deleted at the same time. For which memory
blocks are allocated and de-allocated for the files. Creation of files occupy free blocks and
deletion of file frees blocks. When there is an entry in the free space, it consists of two
parameters- "address of first free disk block (a pointer)" and "a number 'n'".
Example
Let us take an example where a disk has 16 blocks in which some blocks are empty and some
blocks are occupied as given below :
When the "counting technique" is applied, the block number 3 will represent block number 4
because block 3 is the first free block. Then, the block stores the number of free blocks
i.e.i.e. - there are 4 free blocks together. In the same way, the first occupied block number 9
will represent block number 10 and keeps the number of rest occupied blocks i.e.- there are 6
occupied blocks as shown in the above figure.
Advantages
Disadvantage
● In the counting method, the first free block stores the rest free blocks, so it requires
more space.
Disk Scheduling
There are many disk scheduling algorithms that provide the total head movement for various requests to th
All these algorithms are explained using the following requests for the disk
10,95,23,78,80
In the above example, the requests are serviced in the order they appear i.e
10, 95, 23, 78, 80. The seek head is initially positioned at 50 and it starts
from there.
The requests that are closest to the current head are served first before moving away in shortest seek time first sch
In the above example, the requests are serviced in the order 23, 10, 78, 80,
95. The seek head is initially positioned at 50 and it starts from there. 23 is
closest to 50 so it is services first. Then 10 is closer to 23 than 78 so it is
services next. After this 78, 80 and 95 are serviced.
SCAN Scheduling
In this scheduling algorithm, the head moves towards one direction while servicing all the requests in that directio
In the above example, the requests are serviced in the order 23, 10, 78, 80,
95. The head is initially at 50 and moves towards the left while servicing
requests 23 and 10. When it reaches the end of the disk, it starts moving
right and services 78, 80 and 95 as they occur.
LOOK Scheduling
LOOK scheduling algorithm is similar to SCAN scheduling but is its practical version. In this algorithm, the head
In the above example, the requests are serviced in the order 23, 10, 78, 80,
95. The head is initially at 50 and moves towards the left while servicing
requests 23 and 10. When it reaches the last request on the left i.e. 10, it
starts moving right and services 78, 80 and 95 as they occur.