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

OS UNIT 5

A file in an operating system is a collection of related information stored on secondary storage, allowing efficient data management. There are various file structures and access methods, including sequential, direct, and index sequential methods, each with its advantages and disadvantages. Additionally, directory structures, such as single-level, two-level, tree, acyclic graph, and general-graph structures, help organize files and facilitate access in an operating system.

Uploaded by

VENU DINO
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)
34 views

OS UNIT 5

A file in an operating system is a collection of related information stored on secondary storage, allowing efficient data management. There are various file structures and access methods, including sequential, direct, and index sequential methods, each with its advantages and disadvantages. Additionally, directory structures, such as single-level, two-level, tree, acyclic graph, and general-graph structures, help organize files and facilitate access in an operating system.

Uploaded by

VENU DINO
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/ 45

UNIT-5

Definition of a File in an Operating System


A file in an operating system is a collection of related information that is recorded on secondary
storage. It serves as a medium for saving and managing data in a computer system. Files are
essential for storing data in a structured manner, allowing users and applications to create, read,
update, and delete data efficiently.

File Concept in Operating Systems


A file in an operating system is a named collection of related information that is recorded on
secondary storage devices such as magnetic disks, magnetic tapes, and optical disks. Files are
essential for storing data and programs, and they can be organized in various structures and
formats depending on their type and usage.

File Structure and Types

Files can have different structures based on their type:


 Text files: A sequence of characters organized into lines.
 Source files: A sequence of procedures and functions.
 Object files: A sequence of bytes organized into blocks understandable by the machine.
Operating systems like Unix and MS-DOS support various file structures and types, including:
 Ordinary files: Contain user information such as text, databases, or executable programs.
 Directory files: Contain lists of file names and related information.
 Special files: Represent physical devices like disks, terminals, printers, and networks.

1
File Access Methods in Operating System
File access methods in an operating system are the techniques and processes used to read from
and write to files stored on a computer’s storage devices. There are several ways to access this
information in the file. Some systems provide only one access method for files. Other systems,
such as those of IBM, support many access methods, and choosing the right one for a particular
application is a major design problem.
These methods determine how data is organized, retrieved, and modified within a file system.
Understanding file access methods is crucial for efficient data management and system
performance. In this article, we are going to discuss different types of methods to access 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.

Sequential Access Method

2
Key Points related to Sequential Access
 Data is accessed from one record right after another record in an order.
 When we use the read command, it moves ahead pointer by one.
 When we use the write command, it will allocate memory and move the pointer to the end
of the file.
 Such a method is reasonable for tape.
Advantages of Sequential Access Method
 It is simple to implement this file access mechanism.
 It useslexicographic order to quickly access the next entry.
 It is suitable for applications that require access to all records in a file, in a specific order.
 It is less prone to data corruption as the data is written sequentially and not randomly.
 It is a more efficient method for reading large files, as it only reads the required data and
does not waste time reading unnecessary data.
 It is a reliable method forbackup and restore operations, as the data is stored sequentially
and can be easily restored if required.
Disadvantages of Sequential Access Method
 If the file record that needs to be accessed next is not present next to the current record, this
type of file access method is slow.
 Moving a sizable chunk of the file may be necessary to insert a new record.
 It does not allow for quick access to specific records in the file. The entire file must be
searched sequentially to find a specific record, which can be time-consuming.
 It is not well-suited for applications that require frequent updates or modifications to the
file. Updating or inserting a record in the middle of a large file can be a slow and
cumbersome process.
 Sequential access can also result in wasted storage space if records are of varying lengths.
The space between records cannot be used by other records, which can result in inefficient
use of storage.

Direct Access Method


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.

3
Direct Access Method

Advantages of Direct Access Method


 The files can be immediately accessed decreasing the average access time.
 In the direct access method, in order to access a block, there is no need of traversing all the
blocks present before it.
Disadvantages of Direct Access Method
 Complex Implementation : Implementing direct access can be complex, requiring
sophisticated algorithms and data structures to manage and locate records efficiently.
 Higher Storage Overhead : Direct access methods often require additional storage for
maintaining data location information (such as pointers or address tables), which can
increase the overall storage requirements.
Index Sequential method
It is the other method of accessing a file that is built on the top of the sequential access method.
These methods construct an index for the file. The index, like an index in the back of a book,
contains the pointer to the various blocks. To find a record in the file, we first search the index,
and then by the help of pointer we access the file directly.
Key Points Related to Index Sequential Method
 It is built on top of Sequential access.

4
 It control the pointer by using index.
Advantages of Index Sequential Method
 Efficient Searching : Index sequential method allows for quick searches through the index.
 Balanced Performance : It combines the simplicity of sequential access with the speed of
direct access, offering a balanced approach that can handle various types of data access
needs efficiently.
 Flexibility : This method allows both sequential and random access to data, making it
versatile for different types of applications, such as batch processing and real-time
querying.
 Improved Data Management : Indexing helps in better organization and management of
data. It makes data retrieval faster and more efficient, especially in large databases.
 Reduced Access Time : By using an index to directly locate data blocks, the time spent
searching for data within large datasets is significantly reduced.
Disadvantages of Index Sequential Method
 Complex Implementation : The index sequential method is more complex to implement

and maintain compared to simple sequential access methods.


 Additional Storage : Indexes require additional storage space, which can be significant for
large datasets. This extra space can sometimes offset the benefits of faster access.
 Update Overhead : Updating the data can be more time-consuming because both the data
and the indexes need to be updated. This can lead to increased processing time for
insertions, deletions, and modifications.
 Index Maintenance : Keeping the index up to date requires regular maintenance,
especially in dynamic environments where data changes frequently. This can add to the
system’s overhead.

Structures of Directory in Operating System

A directory is a container that is used to contain folders and files. It organizes files and folders
in a hierarchical manner. In other words, directories are like folders that help organize files on a
computer. Just like you use folders to keep your papers and documents in order, the operating

5
system uses directories to keep track of files and where they are stored. Different structures of
directories can be used to organize these files, making it easier to find and manage them.
Understanding these directory structures is important because it helps in efficiently organizing
and accessing files on your computer. Following are the logical structures of a directory, each
providing a solution to the problem faced in the previous type of directory structure.

Different Types of Directory in OS


In an operating system, there are different types of directory structures that help organize and
manage files efficiently.
Directories in an OS can be single-level, two-level, or hierarchical. To gain a comprehensive
understanding of file systems, explore the GATE CS Self-Paced Course which covers operating
systems in great detail.
Each type of directory has its own way of arranging files and directories, offering unique benefits
and features.These are:
 Single-Level Directory
 Two-Level Directory
 Tree Structure/ Hierarchical Structure
 Acyclic Graph Structure
 General-Graph Directory Structure

6
1) Single-Level Directory
The single-level directory is the simplest directory structure. In it, all files are contained in the
same directory which makes it easy to support and understand.
A single level directory has a significant limitation, however, when the number of files increases
or when the system has more than one user. Since all the files are in the same directory, they
must have a unique name. If two users call their dataset test, then the unique name rule violated.

Advantages
 Since it is a single directory, so its implementation is very easy.
 If the files are smaller in size, searching will become faster.
 The operations like file creation, searching, deletion, updating are very easy in such a
directory structure.
 Logical Organization : Directory structures help to logically organize files and directories in
a hierarchical structure. This provides an easy way to navigate and manage files, making it
easier for users to access the data they need.
 Increased Efficiency: Directory structures can increase the efficiency of the file system by
reducing the time required to search for files. This is because directory structures are
optimized for fast file access, allowing users to quickly locate the file they need.
 Improved Security : Directory structures can provide better security for files by allowing
access to be restricted at the directory level. This helps to prevent unauthorized access to
sensitive data and ensures that important files are protected.
 Facilitates Backup and Recovery : Directory structures make it easier to backup and
recover files in the event of a system failure or data loss. By storing related files in the same
directory, it is easier to locate and backup all the files that need to be protected.

7
 Scalability: Directory structures are scalable, making it easy to add new directories and files
as needed. This helps to accommodate growth in the system and makes it easier to manage
large amounts of data.
Disadvantages
 There may chance of name collision because two files can have the same name.
 Searching will become time taking if the directory is large.
 This can not group the same type of files together.
2) Two-Level Directory
As we have seen, a single level directory often leads to confusion of files names among different
users. The solution to this problem is to create a separate directory for each user.
In the two-level directory structure, each user has their own user files directory (UFD). The
UFDs have similar structures, but each lists only the files of a single user. System’s master file
directory (MFD) is searched whenever a new user id is created.

Two-Levels Directory Structure

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.

8
 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.
Disadvantages
 As there is advantage of security, there is also disadvantage that the user cannot share the file
with the other users.
 Unlike the advantage users can create their own files, users don’t have the ability to create
subdirectories.
 Scalability is not possible because one user can’t group the same types of files together.
3) Tree Structure/ Hierarchical Structure
Tree directory structure of operating system is most commonly used in our personal computers.
User can create files and subdirectories too, which was a disadvantage in the previous directory
structures.
This directory structure resembles a real tree upside down, where the root directory is at the
peak. This root contains all the directories for each user. The users can create subdirectories and
even store files in their directory.
A user do not have access to the root directory data and cannot modify it. And, even in this
directory the user do not have access to other user’s directories. The structure of tree directory is
given below which shows how there are files and subdirectories in each user’s directory.

9
Tree/Hierarchical Directory Structure

Advantages
 This directory structure allows subdirectories inside a directory.
 The searching is easier.
 File sorting of important and unimportant becomes easier.
 This directory is more scalable than the other two directory structures explained.
Disadvantages
 As the user isn’t allowed to access other user’s directory, this prevents the file sharing among
users.
 As the user has the capability to make subdirectories, if the number of subdirectories increase
the searching may become complicated.
 Users cannot modify the root directory data.
 If files do not fit in one, they might have to be fit into other directories.
4) Acyclic Graph Structure
As we have seen the above three directory structures, where none of them have the capability to
access one file from multiple directories. The file or the subdirectory could be accessed through
the directory it was present in, but not from the other directory.
This problem is solved in acyclic graph directory structure, where a file in one directory can be
accessed from multiple directories. In this way, the files could be shared in between the users. It
is designed in a way that multiple directories point to a particular directory or file with the help
of links.
In the below figure, this explanation can be nicely observed, where a file is shared between
multiple users. If any user makes a change, it would be reflected to both the users.

Acyclic Graph Structure

10
Advantages
 Sharing of files and directories is allowed between multiple users.
 Searching becomes too easy.
 Flexibility is increased as file sharing and editing access is there for multiple users.
Disadvantages
 Because of the complex structure it has, it is difficult to implement this directory structure.
 The user must be very cautious to edit or even deletion of file as the file is accessed by
multiple users.
 If we need to delete the file, then we need to delete all the references of the file inorder to
delete it permanently.
5) General-Graph Directory Structure
Unlike the acyclic-graph directory, which avoids loops, the general-graph directory can have
cycles, meaning a directory can contain paths that loop back to the starting point. This can make
navigating and managing files more complex.

General Graph Directory Structure

11
In the above image, you can see that a cycle is formed in the User 2 directory. While this
structure offers more flexibility, it is also more complicated to implement.
Advantages of General-Graph Directory
 More flexible than other directory structures.
 Allows cycles, meaning directories can loop back to each other.
Disadvantages of General-Graph Directory
 More expensive to implement compared to other solutions.
 Requires garbage collection to manage and clean up unused files and directories.

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

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

13
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

 Application Programs: This is the topmost layer where users interact with files through
applications. It provides the user interface for file operations like creating, deleting, reading,
writing, and modifying files. Examples include text editors, file browsers, and command-line
interfaces.
 Logical File system – It manages metadata information about a file i.e includes all details
about a file except the actual contents of the file. It also maintains via file control blocks. File
control block (FCB) has information about a file – owner, size, permissions, and location of
file contents.
 File Organization Module – It has information about files, the location of files and their
logical and physical blocks. Physical blocks do not match with logical numbers of logical
blocks numbered from 0 to N. It also has a free space that tracks unallocated blocks.

14
 Basic File system – It Issues general commands to the device driver to read and write
physical blocks on disk. It manages the memory buffers and caches. A block in the buffer can
hold the contents of the disk block and the cache stores frequently used file system metadata.
 I/O Control level – Device drivers act as an interface between devices and OS, they help to
transfer data between disk and main memory. It takes block number as input and as output, it
gives low-level hardware-specific instruction.
 Devices Layer: The bottommost layer, consisting of the actual hardware devices. It performs
the actual reading and writing of data to the physical storage medium. This includes hard
drives, SSDs, optical disks, and other storage devices.
Implementation Issues
 Management of Disc pace: 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.
Key Steps Involved in File System Implementation
File system implementation is a crucial component of an operating system, as it provides an
interface between the user and the physical storage device. Here are the key steps involved in file
system implementation:
 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.

15
 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.
Overall, file system implementation is a complex and critical component of an operating system.
The efficiency and reliability of the file system have a significant impact on the performance and
stability of the entire system.

File Operations in Operating Systems


File operations within an operating system (OS) are essential tasks that involve creating,
modifying, organizing, and managing files and directories. These operations are crucial for the
effective management and manipulation of data stored on various storage devices.

Common File Operations:

Creating Files and Directories


Creating files and directories is a fundamental operation. In Linux, the open() system call is

used to create files, while mkdir() is used to create directories. In

Windows, CreateFile() and CreateDirectory() are used for these purposes1.

Opening, Reading, and Writing Files

16
To perform any operation on a file, it must first be opened. The open() system call in Linux

and CreateFile() in Windows are used to open files. Reading and writing are done

using read() and write() in Linux, and ReadFile() and WriteFile() in Windows1.

Renaming and Deleting Files and Directories


Renaming files and directories can be done using the rename() system call in Linux

and MoveFile() in Windows. Deleting files and directories involves

using unlink() and remove() in Linux, and DeleteFile() and RemoveDirectory() in Windows1.

Copying and Moving Files


Copying files creates duplicates in another location, using cp in Linux and CopyFile() in

Windows. Moving files relocates them from one location to another, using mv in Linux

and MoveFile() in Windows1.

Searching for Files


Searching for files based on specific criteria is done using the find command in Linux

and FindFirstFile() and FindNextFile() in Windows1

Directory Implementation in Operating Systems


Directory implementation in operating systems is crucial for efficient file management and
access. There are several algorithms and data structures used to implement directories, each with
its own advantages and disadvantages. The two primary methods are Linear List and Hash
Table.

Linear List

In this method, all files in a directory are maintained as a singly linked list. Each file contains
pointers to the data blocks assigned to it and the next file in the directory. This approach is
simple to program but can be inefficient in terms of performance.

17
Characteristics
 Creation: When a new file is created, the entire list is checked to ensure the new file name
does not match an existing one. If it doesn't exist, the file can be added at the beginning or end
of the list.
 Deletion: To delete a file, the directory is searched for the file name, and the file is then
removed by releasing the space allocated to it.
 Updating: Any operation on the files (creation, deletion, updating) requires traversing the
entire list, making the system inefficient12.
Disadvantages
 Search Time: Searching for a file requires a linear search through the entire list, which can be
time-consuming.
 Performance: The system becomes inefficient as the list needs to be traversed for every
operation12.
Hash Table

To overcome the drawbacks of the linear list, the hash table approach is used. This method
combines a hash table with linked lists to store directory entries.

18
Characteristics
 Key-Value Pair: For each file in the directory, a key-value pair is generated and stored in the
hash table. The key is determined by applying a hash function to the file name, and it points to
the corresponding file stored in the directory.
 Efficiency: Searching becomes efficient as only the hash table entries are checked using the
key. If an entry is found, the corresponding file is fetched using the value12.
Disadvantages
 Fixed Size: Hash tables generally have a fixed size, which can be a limitation.
 Dependency on Size: The performance of the hash table depends on its size

File Allocation Methods in Operating Systems


File allocation methods are strategies used by operating systems to manage how files are stored
on disk blocks. These methods aim to optimize disk space utilization and provide efficient access
to file blocks. There are three primary file allocation methods: Contiguous Allocation, Linked
Allocation, and Indexed Allocation.

Contiguous Allocation

In contiguous allocation, each file occupies a contiguous set of blocks on the disk. For example,
if a file requires n blocks and starts at block b , it will occupy blocks b, b+1, b+2, ..., b+n-1 .

19
The directory entry for a file contains the address of the starting block and the length of the
allocated portion.
Advantages:
 Supports both sequential and direct access.
 Fast access due to minimal seek time.
Disadvantages:
 Suffers from internal and external fragmentation.
 Difficult to increase file size due to the need for contiguous memory.

Linked Allocation

In linked allocation, each file is a linked list of disk blocks, which can be scattered anywhere on
the disk. The directory entry contains pointers to the starting and ending blocks, and each block
contains a pointer to the next block.
Advantages:
 Flexible in terms of file size.

20
 No external fragmentation.
Disadvantages:
 Slower access due to the need to follow pointers.
 Does not support direct access.
 Extra overhead for storing pointers.

Indexed Allocation

In indexed allocation, a special block known as the index block contains pointers to all the
blocks occupied by a file. Each file has its own index block, and the directory entry contains the
address of this index block.
Advantages:
 Supports direct access to file blocks.
 No external fragmentation.
Disadvantages:
 Higher pointer overhead compared to linked allocation.
 Inefficient for very small files.
 Single index block may not suffice for very large files.
Variations of Indexed Allocation

21
To handle large files, indexed allocation can use:
 Linked Scheme: Links multiple index blocks together.
 Multilevel Index: Uses a hierarchy of index blocks.
 Combined Scheme: Uses an inode that contains pointers to direct and indirect blocks.

File Allocation Table (FAT)

FAT is a file system that uses a table to store information about file allocation. It allows random
access to blocks and reduces head seeks by caching the FAT.
Advantages:
 Supports random access.
 Reduces the impact of bad disk blocks.
Disadvantages:
 Increased FAT size with more entries.
 Potential for internal fragmentation.
Inode

22
In UNIX-based systems, an inode stores metadata about a file and pointers to its blocks. It
supports direct, single indirect, double indirect, and triple indirect blocks.
Advantages:
 Easy file access with metadata and block addresses stored in the inode.
 Supports file renaming without losing address.
Disadvantages:
 System issues when inodes are fully utilized.
In conclusion, each file allocation method has its own strengths and weaknesses, and the choice
of method depends on the specific requirements of the operating system and applications.

Free Space Management in Operating System


Free space management is a critical aspect of operating systems as it involves managing
the available storage space on the hard disk or other secondary storage devices. The operating
system uses various techniques to manage free space and optimize the use of storage devices.
Here are some of the commonly used free space management techniques:

23
Free Space Management Techniques
 Linked Allocation: In this technique, each file is represented by a linked list of disk blocks.
When a file is created, the operating system finds enough free space on the disk and links the
blocks of the file to form a chain. This method is simple to implement but can lead to
fragmentation and waste of space.
 Contiguous Allocation: In this technique, each file is stored as a contiguous block of disk
space. When a file is created, the operating system finds a contiguous block of free space and
assigns it to the file. This method is efficient as it minimizes fragmentation but suffers from
the problem of external fragmentation.
 Indexed Allocation: In this technique, a separate index block is used to store the addresses
of all the disk blocks that make up a file. When a file is created, the operating system creates
an index block and stores the addresses of all the blocks in the file. This method is efficient
in terms of storage space and minimizes fragmentation.
 File Allocation Table (FAT): In this technique, the operating system uses a file allocation
table to keep track of the location of each file on the disk. When a file is created, the
operating system updates the file allocation table with the address of the disk blocks that
make up the file. This method is widely used in Microsoft Windows operating systems.
 Volume Shadow Copy: This is a technology used in Microsoft Windows operating
systems to create backup copies of files or entire volumes. When a file is modified, the
operating system creates a shadow copy of the file and stores it in a separate location. This
method is useful for data recovery and protection against accidental file deletion.
Overall, free space management is a crucial function of operating systems, as it ensures that
storage devices are utilized efficiently and effectively.
The system keeps tracks of the free disk blocks for allocating space to files when they are
created. Also, to reuse the space released from deleting the files, free space management
becomes crucial. The system maintains a free space list which keeps track of the disk blocks that
are not allocated to some file or directory. The free space list can be implemented mainly as:
1. Bitmap or Bit vector
A Bitmap or Bit Vector is series or collection of bits where each bit corresponds to a disk block.
The bit can take two values: 0 and 1: 0 indicates that the block is free and 1 indicates an
allocated block. The given instance of disk blocks on the disk in Figure 1 (where green blocks

24
are allocated) can be represented by a bitmap of 16 bits as: 1111000111111001.

Advantages:
 Simple to understand.
 Finding the first free block is efficient. It requires scanning the words (a group of 8 bits) in a
bitmap for a non-zero word. (A 0-valued word has all bits 0). The first free block is then
found by scanning for the first 1 bit in the non-zero word.
Disadvantages:
 For finding a free block, Operating System needs to iterate all the blocks which is time
consuming.
 The efficiency of this method reduces as the disk size increases.
2. Linked List
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer to the
next free block. The block number of the very first disk block is stored at a separate location on

25
disk and is also cached in memory. In Figure-2,
the free space list head points to Block 5 which points to Block 6, the next free block and so on.
The last free block would contain a null pointer indicating the end of free list. A drawback of this
method is the I/O required for free space list traversal.
Advantages:
 The total available space is used efficiently using this method.
 Dynamic allocation in Linked List is easy, thus can add the space as per the requirement
dynamically.
Disadvantages:
 When the size of Linked List increases, the headache of miniating pointers is also increases.
 This method is not efficient during iteration of each block of memory.
Grouping
This approach stores the address of the free blocks in the first free block. The first free block
stores the address of some, say n free blocks. Out of these n blocks, the first n-1 blocks are
actually free and the last block contains the address of next free n blocks. An advantage of this
approach is that the addresses of a group of free disk blocks can be found easily.
Advantage:
 Finding free blocks in massive amount can be done easily using this method.

26
Disadvantage:
 The only disadvantage is, we need to alter the entire list, if any of the block of the list is
occupied.
Counting
This approach stores the address of the first free disk block and a number n of free contiguous
disk blocks that follow the first block. Every entry in the list would contain:
 Address of first free disk block.
 A number n.
Advantages:
 Using this method, a group of entire free blocks can take place easily and Fastly.
 The list formed in this method is especially smaller in size.
Disadvantage:
 The first free block in this method, keeps account of other free blocks. Thus, due to that one
block the space requirement is more.
Advantages of Free Space Management Techniques
 Efficient Use of Storage Space: Free space management techniques help to optimize the use
of storage space on the hard disk or other secondary storage devices.
 Easy to Implement: Some techniques, such as linked allocation, are simple to implement
and require less overhead in terms of processing and memory resources.
 Faster Access to Files: Techniques such as contiguous allocation can help to reduce disk
fragmentation and improve access time to files.
Disadvantages of Free Space Management Techniques
 Fragmentation: Techniques such as linked allocation can lead to fragmentation of disk
space, which can decrease the efficiency of storage devices.
 Overhead: Some techniques, such as indexed allocation, require additional overhead in
terms of memory and processing resources to maintain index blocks.
 Limited scalability: Some techniques, such as FAT, have limited scalability in terms of the
number of files that can be stored on the disk.
 Risk of data loss: In some cases, such as with contiguous allocation, if a file becomes
corrupted or damaged, it may be difficult to recover the data.

27
 Overall, the choice of free space management technique depends on the specific
requirements of the operating system and the storage devices being used. While some
techniques may offer advantages in terms of efficiency and speed, they may also have
limitations and drawbacks that need to be considered.

File System Mounting in Operating Systems


File system mounting is a crucial process in operating systems that allows users to access and
organize files from various storage devices. It involves associating a storage device with a
directory in the file system, enabling the operating system to integrate the device's files and
directories into the user's file system.

What is File System Mounting?

Mounting is the process by which the operating system adds directories and files from a storage
device to the user's computer file system. This is done by attaching the file system to an empty
directory, known as the mount point. Once mounted, the data on the storage device can be
accessed through the system file manager1.
Terminologies Used in File System Mounting

 File System: The method used by the operating system to manage data storage on a storage
device, allowing users to access and organize directories and files efficiently1.
 Device Name: An identifier given to a storage partition, such as "D:" in Windows1.
 Mount Point: An empty directory where the file system is added during the mounting
process1.
Mounting in Different Operating Systems

Linux/Unix-based OS
In Linux/Unix-based systems, mounting is done using the mount command. For example, to

mount /dev/sdb1 to an existing directory /mnt/mydisk , you would use:

sudo mount /dev/sdb1 /mnt/mydisk

After using the mounted file system, it should be unmounted using:


sudo umount /mnt/mydisk

28
This ensures that the file system is properly detached from the directory1.
Windows OS
In Windows, mounting is typically automatic. When an external storage device is connected,
Windows detects the file system and assigns it a drive letter, such as "E:". Users can then access
the drive through File Explorer1.
Mac OS
Similar to Windows, Mac OS automatically mounts external storage devices, making them
accessible via Finder. Advanced users can also use the diskutil command in Terminal to

manually mount and unmount drives:


diskutil mount /dev/disk2s1

diskutil unmount /dev/disk2s1

This provides flexibility for managing storage devices1.


Importance of File System Mounting

The primary purpose of file system mounting is to make files and directories accessible to users.
It allows the operating system to integrate various storage devices seamlessly, providing a
unified view of all available data1.
In Linux, the /etc/fstab file contains information about file systems that should be automatically

mounted at boot time. This file helps in managing persistent mounts across reboots

29
File Sharing in OS

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.

30
Primary Terminology Related to File Sharing
Let's see what are the various ways to achieve this, but there are some important terminologies
one should know beforehand. Let's discuss those primary terminologies first:

 Folder/Directory: It is basically like a container for all of our files on a computer. The
folder can contain files and even other folders maintaining like hierarchical structure for
organizing data.
 Networking: It is involved in connecting computers or devices where we need to share the
resources. Networks can be local (LAN) or global (Internet).
 IP Address: It is numerical data given to every connected device on the network
 Protocol: It is given as the set of rules which drives the communication between devices on a
network. In the context of file sharing, protocols define how files are transferred between
computers.
 File Transfer Protocol (FTP): FTP is a standard network protocol used to transfer files
between a client and a server on a computer network.
Various Ways to Achieve File Sharing
Let's see the various ways through which we can achieve file sharing in an OS.
1. Server Message Block (SMB)
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
system.orate 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.

SMB File Sharing

31
SMB and it's implementation

2. Network File System (NFS)


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.
Example: Many Programmer/Universities/Research Institution uses Unix/Linux based
Operating System. The Institutes puts up a global server datasets using NFS. The Researchers
and students can access these shared directories and everyone can collaborate on it.

NFS File Sharing

NFS and it's architecture


3. File Transfer Protocol (FTP)

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.

32
Read more about FTP: FTP and it's implementation

FTP File Sharing

4. Cloud-Based File Sharing


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.

Cloud Based File Sharing

These all file sharing methods serves different purpose and needs according to the requirements
and flexibility of the users based on the operating system.
Protection in File System
In computer systems, alot of user’s information is stored, the objective of the operating system
is to keep safe the data of the user from the improper access to the system. Protection can be
provided in number of ways. For a single laptop system, we might provide protection by
locking the computer in a desk drawer or file cabinet. For multi-user systems, different
mechanisms are used for the protection.

33
Types of Access :
The files which have direct access of the any user have the need of protection. The files which
are not accessible to other users doesn’t require any kind of protection. The mechanism of the
protection provide the facility of the controlled access by just limiting the types of access to the
file. Access can be given or not given to any user depends on several factors, one of which is
the type of access required. Several different types of operations can be controlled:

 Read – Reading from a file.


 Write – Writing or rewriting the file.
 Execute – Loading the file and after loading the execution process starts.
 Append – Writing the new information to the already existing file, editing must be end at
the end of the existing file.
 Delete – Deleting the file which is of no use and using its space for the another data.
 List – List the name and attributes of the file.
Operations like renaming, editing the existing file, copying; these can also be controlled. There
are many protection mechanism. each of them mechanism have different advantages and
disadvantages and must be appropriate for the intended application.

Access Control :
There are different methods used by different users to access any file. The general way of
protection is to associate identity-dependent access with all the files and directories an list
called access-control list (ACL) which specify the names of the users and the types of access
associate with each of the user. The main problem with the access list is their length. If we
want to allow everyone to read a file, we must list all the users with the read access. This
technique has two undesirable consequences:
Constructing such a list may be tedious and unrewarding task, especially if we do not know in
advance the list of the users in the system.

Previously, the entry of the any directory is of the fixed size but now it changes to the variable
size which results in the complicates space management. These problems can be resolved by
use of a condensed version of the access list. To condense the length of the access-control list,
many systems recognize three classification of users in connection with each file:

 Owner – Owner is the user who has created the file.

34
 Group – A group is a set of members who has similar needs and they are sharing the same
file.
 Universe – In the system, all other users are under the category called universe.
The most common recent approach is to combine access-control lists with the normal general
owner, group, and universe access control scheme. For example: Solaris uses the three
categories of access by default but allows access-control lists to be added to specific files and
directories when more fine-grained access control is desired.

Other Protection Approaches:


The access to any system is also controlled by the password. If the use of password is random
and it is changed often, this may be result in limit the effective access to a file.

The use of passwords has a few disadvantages:

 The number of passwords are very large so it is difficult to remember the large passwords.
 If one password is used for all the files, then once it is discovered, all files are accessible;
protection is on all-or-none basis.

File System Protection in Operating Systems


File system protection is a critical aspect of operating systems (OS) that ensures the security and
integrity of data stored on a computer. It involves various mechanisms to prevent unauthorized
access, misuse, or modification of files and directories.

Access Control and Types of Access

One of the primary methods of file system protection is through access control. This mechanism
restricts the types of operations that can be performed on files by different users. Access control
can be implemented using an access-control list (ACL), which specifies user names and the
corresponding types of access allowed for each user1.
The types of access that can be controlled include:
 Read: Allows reading from a file.
 Write: Permits writing or rewriting the file.
 Execute: Enables loading and execution of a file.
 Append: Allows writing new information at the end of an existing file.

35
 Delete: Permits deletion of a file.
 List: Enables listing the names and attributes of files.
Additional operations like renaming, editing, and copying can also be controlled through access
control mechanisms.
User Classifications

To streamline the access-control process, systems often recognize three classifications of users in
relation to each file:
 Owner: The user who created the file.
 Group: A set of users sharing the same file.
 Universe: All other users in the system.
This approach is commonly used in conjunction with ACLs to provide a balance between
detailed access control and manageability. For instance, Solaris uses the three categories by
default but allows ACLs to be added to specific files and directories for more granular control1.
Passwords and Protection

Another layer of protection is the use of passwords. Passwords can limit access to files
effectively if they are changed regularly and are not easily guessable. However, this method has
drawbacks, such as the difficulty in remembering multiple complex passwords and the risk of all
files becoming accessible if a single password is compromised1.
Advantages and Disadvantages

The advantages of system protection include ensuring data security, preventing unauthorized
access, and maintaining system integrity. However, implementing system protection can be
complex, potentially slow down system performance, and may lead to compatibility issues with
some applications or hardware2

Protection in File System


In computer systems, alot of user’s information is stored, the objective of the operating
system is to keep safe the data of the user from the improper access to the system. Protection
can be provided in number of ways. For a single laptop system, we might provide protection by

36
locking the computer in a desk drawer or file cabinet. For multi-user systems, different
mechanisms are used for the protection.

Types of Access :
The files which have direct access of the any user have the need of protection. The files which
are not accessible to other users doesn’t require any kind of protection. The mechanism of the
protection provide the facility of the controlled access by just limiting the types of access to the
file. Access can be given or not given to any user depends on several factors, one of which is
the type of access required. Several different types of operations can be controlled:

 Read – Reading from a file.


 Write – Writing or rewriting the file.
 Execute – Loading the file and after loading the execution process starts.
 Append – Writing the new information to the already existing file, editing must be end at
the end of the existing file.
 Delete – Deleting the file which is of no use and using its space for the another data.
 List – List the name and attributes of the file.
Operations like renaming, editing the existing file, copying; these can also be controlled. There
are many protection mechanism. each of them mechanism have different advantages and
disadvantages and must be appropriate for the intended application.

Access Control :
There are different methods used by different users to access any file. The general way of
protection is to associate identity-dependent access with all the files and directories an list
called access-control list (ACL) which specify the names of the users and the types of access
associate with each of the user. The main problem with the access list is their length. If we
want to allow everyone to read a file, we must list all the users with the read access. This
technique has two undesirable consequences:
Constructing such a list may be tedious and unrewarding task, especially if we do not know in
advance the list of the users in the system.

Previously, the entry of the any directory is of the fixed size but now it changes to the variable
size which results in the complicates space management. These problems can be resolved by

37
use of a condensed version of the access list. To condense the length of the access-control list,
many systems recognize three classification of users in connection with each file:

 Owner – Owner is the user who has created the file.


 Group – A group is a set of members who has similar needs and they are sharing the same
file.
 Universe – In the system, all other users are under the category called universe.
The most common recent approach is to combine access-control lists with the normal general
owner, group, and universe access control scheme. For example: Solaris uses the three
categories of access by default but allows access-control lists to be added to specific files and
directories when more fine-grained access control is desired.

Other Protection Approaches:


The access to any system is also controlled by the password. If the use of password is random
and it is changed often, this may be result in limit the effective access to a file.

The use of passwords has a few disadvantages:

 The number of passwords are very large so it is difficult to remember the large passwords.
 If one password is used for all the files, then once it is discovered, all files are accessible;
protection is on all-or-none basis.

Domain of Protection in Operating Systems


Protection in operating systems (OS) is crucial for ensuring the safety and security of system
resources and data. It involves controlling access to resources and preventing unauthorized
access. One of the key concepts in OS protection is the domain of protection.

Definition and Importance

The domain of protection refers to the set of resources that are controlled by a particular
protection mechanism. In an OS, a domain can be defined as a set of objects (resources like files,
memory, and I/O devices) that are accessed by a set of subjects (entities like processes, users,
and groups). Each domain has specific rules that govern access to its objects by its subjects1.

38
Protection is essential in a multiuser environment where multiple users share computer resources
such as CPU and memory. It ensures that each process can only access the resources necessary to
fulfill its task, thereby preventing unauthorized access and potential security breaches2.
Components of Domain of Protection

1. Objects and Operations: Each domain consists of a set of objects and the operations that can
be performed on them. For example, a domain element can be described as <object, {set of

operations on object}> . Objects may share common operations, leading to overlapping

domains1.
2. Process and Procedure Association: A domain can consist of a process, procedure, or user.
If a domain corresponds to a procedure, changing the domain would mean changing the
procedure ID. Processes can switch from one domain to another if they have the necessary
access rights1.
3. Fixed and Dynamic Association: Fixed Association: All access rights are given to processes
at the beginning, but this can lead to excessive access rights for domain switching. Therefore,
dynamic methods are often used to change the contents of the domain^2^. Dynamic
Association: Processes can switch dynamically, creating new domains as needed^2^.
Examples of Domains of Protection

1. Memory Protection: This hardware mechanism separates different parts of memory,


ensuring that each process can only access its own memory space. This prevents one process
from accessing or modifying another process's memory, protecting the system from malware
and other malicious attacks3.
2. Process Isolation: This software mechanism ensures that each process can only access its
allocated resources, such as CPU time, memory, and I/O devices, without interfering with
other processes. It is critical for ensuring system reliability and stability3.
3. Privilege Levels: These hardware-based mechanisms establish distinct levels of access to
system resources. For example, user mode and kernel mode in modern operating systems.
User mode has limited access, while kernel mode has full access to system resources3.
Challenges in Implementing Protection Mechanisms

39
1. Complexity of Modern Operating Systems: As OSes become more complex, identifying
and securing all potential vulnerabilities becomes more challenging. Continuous monitoring
and updating of security measures are required to stay ahead of emerging threats3.
2. Trade-off Between Security and Usability: Protection mechanisms can make it more
difficult for users to access the resources they need, creating a balance between security and
usability3.
3. Overhead and Complexity: Protection mechanisms can introduce additional overhead and
complexity, impacting system performance and resource utilization3.
In conclusion, the domain of protection is a fundamental concept in OS protection, ensuring that
resources are accessed according to defined policies and preventing unauthorized access. It
involves various mechanisms like memory protection, process isolation, and privilege levels,
each playing a crucial role in maintaining system security and reliability

Access Matrix in Operating System


An Access Matrix is a digital model utilized to control and manage permissions. This
model defines the rights each user has for different resources. In simple terms, it’s a table that
shows what actions an individual or a group of users can perform on specific objects within a
system.
It represents the access control mechanism that specifies which actions (e.g., read, write,
execute) are allowed or denied for each subject on each object.
Different Types of Rights
There are different types of rights the files can have. The most common ones are:
1. Read- This is a right given to a process in a domain that allows it to read the file.
2. Write- Process in the domain can be written into the file.
3. Execute- The process in the domain can execute the file.
4. Print- Process in the domain only has access to a printer.
Sometimes, domains can have more than one right, i.e. combination of rights mentioned above.
Let us now understand how an access matrix works from the example given below.

40
F1 F2 F3 Printer

D1 read read

D2 print

D3 read execute

D4 read write read write

Observations of Above Matrix


 There are four domains and four objects– three files(F1, F2, F3) and one printer.
 A process executing in D1 can read files F1 and F3.
 A process executing in domain D4 has same rights as D1 but it can also write on files.
 Printer can be accessed by only one process executing in domain D2.
 A process executing in domain D3 has the right to read file F2 and execute file F3.
Mechanism of Access Matrix
The mechanism of access matrix consists of many policies and semantic properties. Specifically,
we must ensure that a process executing in domain Di can access only those objects that are
specified in row i. Policies of access matrix concerning protection involve which rights should be
included in the (i, j)th entry. We must also decide the domain in which each process executes.
This policy is usually decided by the operating system. The users decide the contents of the
access-matrix entries. Association between the domain and processes can be either static or
dynamic. Access matrix provides a mechanism for defining the control for this association
between domain and processes.
Switch operation: When we switch a process from one domain to another, we execute a switch
operation on an object(the domain). We can control domain switching by including domains
among the objects of the access matrix. Processes should be able to switch from one domain (Di)
to another domain (Dj) if and only if a switch right is given to access(i, j). This is explained
using an example below:

41
F1 F2 F3 Printer D1 D2 D3 D4

D1 read read switch

D2 print switch switch

D3 read execute

D4 read write read write switch

According to the above matrix, a process executing in domain D2 can switch to domain D3 and
D4. A process executing in domain D4 can switch to domain D1 and process executing in
domain D1 can switch to domain D2.
Implementations
There are various methods of implementing the access matrix in the operating system such as.
1. Global Table
2. Access Lists for Objects
3. Capability Lists for Domains
Global Table:
A single table with rows and columns, where rows represents subjects and columns represents
objects. Each cell of the global table contains the access for the subject-object pair.
Example
class AccessMatrix:
def __init__(self, subjects, objects):
self.subjects = subjects
self.objects = objects
self.matrix = {subject: {obj: set() for obj in objects} for subject in subjects}

def set_permission(self, subject, obj, right):


if subject in self.matrix and obj in self.matrix[subject]:

42
self.matrix[subject][obj].add(right)

def check_permission(self, subject, obj, right):


return right in self.matrix.get(subject, {}).get(obj, set())

# Example usage
subjects = ['user1', 'user2']
objects = ['file1', 'file2']
am = AccessMatrix(subjects, objects)
am.set_permission('user1', 'file1', 'read')
print(am.check_permission('user1', 'file1', 'read')) # Output: True
print(am.check_permission('user2', 'file1', 'read')) # Output: False

Access Lists for Objects:


This is a column wise representation of access matrix and allows subjects to access specific
objects according to the access list.
Example
class ACL:
def __init__(self):
self.acl = {}

def add_object(self, obj):


if obj not in self.acl:
self.acl[obj] = {}

def set_permission(self, obj, subject, right):


if obj in self.acl:
if subject not in self.acl[obj]:
self.acl[obj][subject] = set()
self.acl[obj][subject].add(right)

43
def check_permission(self, obj, subject, right):
return right in self.acl.get(obj, {}).get(subject, set())

# Example usage
acl = ACL()
acl.add_object('file1')
acl.set_permission('file1', 'user1', 'read')
print(acl.check_permission('file1', 'user1', 'read')) # Output: True
print(acl.check_permission('file1', 'user2', 'read')) # Output: False

Capability Lists for Domains:


This is a row wise representation of access matrix. Each subject has a capability list, where each
capability specifies an object and the rights the subject has to that object.
Example
class Capability:
def __init__(self):
self.capabilities = {}

def add_subject(self, subject):


if subject not in self.capabilities:
self.capabilities[subject] = {}

def add_capability(self, subject, obj, right):


if subject in self.capabilities:
if obj not in self.capabilities[subject]:
self.capabilities[subject][obj] = set()
self.capabilities[subject][obj].add(right)

def check_capability(self, subject, obj, right):


return right in self.capabilities.get(subject, {}).get(obj, set())

44
# Example usage
cap = Capability()
cap.add_subject('user1')
cap.add_capability('user1', 'file1', 'read')
print(cap.check_capability('user1', 'file1', 'read')) # Output: True
print(cap.check_capability('user2', 'file1', 'read')) # Output: False

45

You might also like