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

Operating System 4

Operating System 4

Uploaded by

ashirai8090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Operating System 4

Operating System 4

Uploaded by

ashirai8090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Class Notes

UNIT - 4
File System Implementation in Operating
System

A file is a collection of related information. The file system resides on


secondary storage and provides efficient and convenient access to the disk
by allowing data to be stored, located, and retrieved. File system
implementation in an operating system refers to how the file system manages
the storage and retrieval of data on a physical storage device such as a hard
drive, solid-state drive, or flash drive.

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

Components of File System Implementation

The file system implementation includes several components, including:

● 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.
3

Different Types of File Systems

There are several types of file systems, each designed for specific purposes
and compatible with different operating systems. Some common file system
types include:

● FAT32 (File Allocation Table 32): Commonly used in older versions


of Windows and compatible with various operating systems.
● NTFS (New Technology File System): Used in modern Windows
operating systems, offering improved performance, reliability, and
security features.
● ext4 (Fourth Extended File System): Used in Linux distributions,
providing features such as journaling, large file support, and
extended file attributes.
● HFS+ (Hierarchical File System Plus): Used in macOS systems prior
to macOS High Sierra, offering support for journaling and case-
insensitive file names.
● APFS (Apple File System): Introduced in macOS High Sierra and the
default file system for macOS and iOS devices, featuring enhanced
performance, security, and snapshot capabilities.
● ZFS (Zettabyte File System): A high-performance file system known
for its advanced features, including data integrity, volume
management, and efficient snapshots.
4

Layers in File System

A file system in an operating system is organized into multiple layers, each


responsible for different aspects of file management and storage. Here are
the key layers in a typical file system:

Layers in File System

What is the role of the directory structure in a file system?

he directory structure organizes files and directories into a hierarchical


structure, making it easier to manage and locate files. It allows users to
navigate the file system and perform operations like creating, deleting, and
moving files.
5

What is the difference between contiguous and non-contiguous file


allocation?

● Contiguous Allocation: Files are stored in continuous blocks of


memory, which makes access fast but can lead to fragmentation.
● Non-Contiguous Allocation: Files are divided into blocks that can be
stored in different locations, reducing fragmentation but increasing
complexity in managing file locations.

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:
6

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:
7

● 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
8

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
9

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

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.

For files that are very large, single index block may not be able to hold all the
pointers.
Following mechanisms can be used to resolve this:

1. Linked scheme: This scheme links two or more index blocks


together for holding the pointers. Every index block would then
contain a pointer or the address to the next index block.
2. Multilevel index: In this policy, a first level index block is used to
point to the second level index blocks which inturn points to the disk
blocks occupied by the file. This can be extended to 3 or more levels
depending on the maximum file size.
3. Combined Scheme: In this scheme, a special block called the Inode
(information Node) contains all the information about the file such
as the name, size, authority, etc and the remaining space of Inode is
used to store the Disk Block addresses which contain the actual file
as shown in the image below. The first few of these pointers in Inode
point to the direct blocks i.e the pointers contain the addresses of
the disk blocks that contain data of the file. The next few pointers
11

point to indirect blocks. Indirect blocks may be single indirect, double


indirect or triple indirect. Single Indirect block is the disk block that
does not contain the file data but the disk address of the blocks that
contain the file data. Similarly, double indirect blocks do not contain
the file data but the disk address of the blocks that contain the
address of the blocks containing the file data.

Free Space Management in OS


As we know, there is limited space (hard disk) in our system. So, there should be proper
utilization of space or memory available in our system.

The Operating system works to allocate free space to the files when a file is created. It
also creates a free void space when a file is deleted from the system. For doing all
these tasks and managing spaces in our system, the operating system works with the
help of a free space management system and allocates and de-allocates memory
spaces simultaneously. In this article, we are going to learn about these concepts.
12

What is Free Space Management in OS?


There is a system software in an operating system that manipulates and keeps a track
of free spaces to allocate and de-allocate memory blocks to files, this system is called a
file management system in an operating system". There is a free space list in an
operating system that maintains the record of free blocks.

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.

Methods of Free Space Management in OS


It is not easy work for an operating system to allocate and de-allocate memory blocks
(managing free space) simultaneously. The operating system uses various methods for
adding free space and freeing up space after deleting a file. There are various methods
using which a free space list can be implemented. We are going to explain them below-

Bitmap or Bit Vector


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.

Let us take an example:

Given below is a diagrammatic representation of a disk in which there are 16 blocks.


There are some free and some occupied blocks present. The upper part is showing
block number. Free blocks are represented by 1 and occupied blocks are represented by
0.
13

"Free block number" can be defined as that block which does not contain any value,

i.e., they are free blocks. The formula to find a free block number is :

[Block number = (number of bits per words)*(number of 0-value word) + Offset of first 1
bit ]

We will consider the first 8 bits group (00111100011110) to constitute a non-zero


word since all bits are not 0 here. Non-zero word is that word that contains the bit value
1 (block that is not free). Here, the first non-zero word is the third block of the group. So,
the offset will be 3.

Hence, the block number

=8∗0+3=3

=8∗0+3=3

Advantages of Bit vector method

● Simple and easy to understand.


● Consumes less memory.
● It is efficient to find free space.

Disadvantages of the Bit vector method

● The operating system goes through all the blocks until it finds a free block.
(block whose bit is '0').
14

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

Advantage of the Linked list

● In this method, available space is used efficiently.


● As there is no size limit on a linked list, a new free space can be added easily.

Disadvantages

● In this method, the overhead of maintaining the pointer appears.


● The Linked list is not efficient when we need to reach every block of memory.

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
15

3,4,5,6,9,10,11,12,13,

3,4,5,6,9,10,11,12,13,, and

14

14. And occupied block numbers are

1,2,7,8,15,

1,2,7,8,15, and

16

16

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.

Advantage of the Grouping method

1. By using this method, we can easily find addresses of a large number of free
blocks easily and quickly.

Disadvantage

1. We need to change the entire list if one block gets occupied.

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
16

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

● In this method, a bunch of free blocks takes place fastly.


● The list is smaller in size.

Disadvantage

● In the counting method, the first free block stores the rest free blocks, so it
requires more space.

Advantages and Disadvantages of Free Space


Management Techniques in Operating Systems
Free space management is a critical component of operating systems, aiming to
optimize the utilization of storage space. Below are the general advantages and
disadvantages associated with these techniques.
17

Advantages

● Efficient Use of Storage Space: These techniques ensure optimal utilization of


available space on hard disks and other secondary storage devices, minimizing
wastage.
● Ease of Implementation: Some methods, like linked allocation, are
straightforward and require minimal overhead in terms of processing and
memory resources.
● Faster File Access: Techniques such as contiguous allocation help in reducing
disk fragmentation, leading to quicker access times for files and improved
system performance.

Disadvantages

● Fragmentation: Certain techniques, particularly linked allocation, can lead to


fragmentation of disk space, reducing the efficiency of storage operations.
● Overhead: Techniques like indexed allocation may introduce additional overhead,
necessitating more memory and processing power to maintain structures like
index blocks.
● Limited Scalability: Some methods, such as the File Allocation Table (FAT), may
not scale well, limiting the number of files that can be efficiently managed on the
disk.
● Risk of Data Loss: In methods like contiguous allocation, if a file becomes
corrupted or damaged, it may be challenging to recover the entire data, leading to
potential data loss.

Magnetic Disk Structure in OS


More about Magnetic Disk Structure
Note – The length of the tracks near the centre is less than the length of the tracks farther from the
centre.

● Each track is further divided into sectors.


● Spindle revolves the platters and is controlled by r/w unit of OS. Some advanced spindles
have capability to only revolve a particular disk and keep others intact.
18

● Arm Assembly is there which keeps a pointy r/w head on each disk to read of write on a
particular disk.
● A world cylinder may also be used at times to refer disk stack.

The speed of the disk is measured as two parts:

● Transfer rate: This is the rate at which the data moves from disk to the computer.
● Random access time: It is the sum of the seek time and rotational latency.

Seek time is the time taken by the arm to move to the required track. Rotational latency is defined as
the time taken by the arm to reach the required sector in the track.

Transfer time is the time to transfer the data. It depends on the rotating speed of the disk and
number of bytes to be transferred.

Disk Scheduling Algorithms

Disk scheduling is a technique operating systems use to manage the order in


which disk I/O (input/output) requests are processed. Disk scheduling is also
known as I/O Scheduling. The main goals of disk scheduling are to optimize
the performance of disk operations, reduce the time it takes to access data
and improve overall system efficiency.

In this article, we will explore the different types of disk scheduling algorithms
and their functions. By understanding and implementing these algorithms, we
can optimize system performance and ensure faster data retrieval.
19

What are Disk Scheduling Algorithms?

Disk scheduling algorithms are crucial in managing how data is read from and
written to a computer’s hard disk. These algorithms help determine the order
in which disk read and write requests are processed, significantly impacting
the speed and efficiency of data access. Common disk scheduling methods
include First-Come, First-Served (FCFS), Shortest Seek Time First (SSTF),
SCAN, C-SCAN, LOOK, and C-LOOK. By understanding and implementing these
algorithms, we can optimize system performance and ensure faster data
retrieval.

Importance of Disk Scheduling in Operating System

● Multiple I/O requests may arrive by different processes and only one
I/O request can be served at a time by the disk controller. Thus other
I/O requests need to wait in the waiting queue and need to be
scheduled.
● Two or more requests may be far from each other so this can result
in greater disk arm movement.
● Hard drives are one of the slowest parts of the computer system and
thus need to be accessed in an efficient manner.

Key Terms Associated with Disk Scheduling

● Seek Time: Seek time is the time taken to locate the disk arm to a
specified track where the data is to be read or written. So the disk
scheduling algorithm that gives a minimum average seek time is
better.
20

● Rotational Latency: Rotational Latency is the time taken by the


desired sector of the disk to rotate into a position so that it can
access the read/write heads. So the disk scheduling algorithm that
gives minimum rotational latency is better.
● Transfer Time: Transfer time is the time to transfer the data. It
depends on the rotating speed of the disk and the number of bytes to
be transferred.
● Disk Access Time:

Disk Access Time = Seek Time + Rotational Latency + Transfer Time

Total Seek Time = Total head Movement * Seek Time

Disk Access Time and Disk Response Time

● Disk Response Time: Response Time is the average time spent by a


request waiting to perform its I/O operation. The average Response
time is the response time of all requests. Variance Response Time is
the measure of how individual requests are serviced with respect to
average response time. So the disk scheduling algorithm that gives
minimum variance response time is better.

Goal of Disk Scheduling Algorithms


21

● Minimize Seek Time


● Maximize Throughput
● Minimize Latency
● Fairness
● Efficiency in Resource Utilization

Disk Scheduling Algorithms

There are several Disk Several Algorithms. We will discuss in detail each one
of them.

● FCFS (First Come First Serve)


● SSTF (Shortest Seek Time First)
● SCAN
● C-SCAN
● LOOK
● C-LOOK
● RSS (Random Scheduling)
● LIFO (Last-In First-Out)
● N-STEP SCAN
● F-SCAN

1. FCFS (First Come First Serve)


22

FCFS is the simplest of all Disk Scheduling Algorithms. In FCFS, the requests
are addressed in the order they arrive in the disk queue. Let us understand this
with the help of an example.

First Come First Serve

Example:

Suppose the order of request is- (82,170,43,140,24,16,190)


And current position of Read/Write head is: 50

So, total overhead movement (total distance covered by the disk


arm) =

(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642
23

Advantages of FCFS

Here are some of the advantages of First Come First Serve.

● Every request gets a fair chance


● No indefinite postponement

Disadvantages of FCFS

Here are some of the disadvantages of First Come First Serve.

● Does not try to optimize seek time


● May not provide the best possible service

2. SSTF (Shortest Seek Time First)

In SSTF (Shortest Seek Time First), requests having the shortest seek time are
executed first. So, the seek time of every request is calculated in advance in
the queue and then they are scheduled according to their calculated seek
time. As a result, the request near the disk arm will get executed first. SSTF is
certainly an improvement over FCFS as it decreases the average response
time and increases the throughput of the system. Let us understand this with
the help of an example.
24

Example:

Shortest Seek Time First

Suppose the order of request is- (82,170,43,140,24,16,190)


And current position of Read/Write head is: 50

So,

total overhead movement (total distance covered by the disk


arm) =

(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208

Advantages of Shortest Seek Time First

Here are some of the advantages of Shortest Seek Time First.

● The average Response Time decreases


● Throughput increases

Disadvantages of Shortest Seek Time First

Here are some of the disadvantages of Shortest Seek Time First.


25

● Overhead to calculate seek time in advance


● Can cause Starvation for a request if it has a higher seek time as
compared to incoming requests
● The high variance of response time as SSTF favors only some
requests

3. SCAN

In the SCAN algorithm the disk arm moves in a particular direction and
services the requests coming in its path and after reaching the end of the disk,
it reverses its direction and again services the request arriving in its path. So,
this algorithm works as an elevator and is hence also known as an elevator
algorithm. As a result, the requests at the midrange are serviced more and
those arriving behind the disk arm will have to wait.

Example:

SCAN Algorithm
26

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

Therefore, the total overhead movement (total distance covered by the disk
arm) is calculated as

= (199-50) + (199-16) = 332

Advantages of SCAN Algorithm

Here are some of the advantages of the SCAN Algorithm.

● High throughput
● Low variance of response time
● Average response time

Disadvantages of SCAN Algorithm

Here are some of the disadvantages of the SCAN Algorithm.

● Long waiting time for requests for locations just visited by disk arm

4. C-SCAN

In the SCAN algorithm, the disk arm again scans the path that has been
scanned, after reversing its direction. So, it may be possible that too many
requests are waiting at the other end or there may be zero or few requests
pending at the scanned area.
27

These situations are avoided in the CSCAN algorithm in which the disk arm
instead of reversing its direction goes to the other end of the disk and starts
servicing the requests from there. So, the disk arm moves in a circular fashion
and this algorithm is also similar to the SCAN algorithm hence it is known as
C-SCAN (Circular SCAN).

Example:

Circular SCAN

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

So, the total overhead movement (total distance covered by the disk arm) is
calculated as:

=(199-50) + (199-0) + (43-0) = 391


28

Advantages of C-SCAN Algorithm

Here are some of the advantages of C-SCAN.

● Provides more uniform wait time compared to SCAN.

5. LOOK

LOOK Algorithm is similar to the SCAN disk scheduling algorithm except for
the difference that the disk arm in spite of going to the end of the disk goes
only to the last request to be serviced in front of the head and then reverses
its direction from there only. Thus it prevents the extra delay which occurred
due to unnecessary traversal to the end of the disk.

Example:

LOOK Algorithm
29

Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the


Read/Write arm is at 50, and it is also given that the disk arm should move
“towards the larger value”.

So, the total overhead movement (total distance covered by the disk arm) is
calculated as:

= (190-50) + (190-16) = 314

6. C-LOOK

As LOOK is similar to the SCAN algorithm, in a similar way, C-LOOK is similar


to the CSCAN disk scheduling algorithm. In CLOOK, the disk arm in spite of
going to the end goes only to the last request to be serviced in front of the
head and then from there goes to the other end’s last request. Thus, it also
prevents the extra delay which occurred due to unnecessary traversal to the
end of the disk.

Example:

1. Suppose the requests to be addressed are-82,170,43,140,24,16,190.


And the Read/Write arm is at 50, and it is also given that the disk arm
should move “towards the larger value”
30

C-LOOK

So, the total overhead movement (total distance covered by the disk arm) is
calculated as

= (190-50) + (190-16) + (43-16) = 341

You might also like