Вы находитесь на странице: 1из 42

File Management | 1

UNIT 5: File Management

Structure

5.0 Objectives
5.1 Introduction
5.2 File Concept
5.3 Directory Structure
5.4 File Sharing
5.5 File System Structure
5.6 File System Implementation
5.7 Partitions and Mounting
5.8 Virtual File System
5.9 Directory Implementation
5.10 Allocation methods
5.11 Disk Scheduling Algorithms
5.12 Network File System
5.13 I/O system
5.14 Summary
5.15 Exercise
5.16 Suggested Readings

5.0 Objectives

At the end of this chapter you will know:

File system structure and Directory structure


File System implementation and Directory implementation
Access methods and allocation methods
Virtual file system and Network file system
Disk scheduling algorithms
I/O system
2 | Operating System

5.1 Introduction

Computer information is stored on different types of storage devices like magnetic tapes, magnetic
disks and optical disks. To facilitate simple and easy use of the information, it is stored in a
systematic way. The logical storage unit used to hold the information is referred to as a file.
Operating system maps the files onto physical devices. Unlike the main memory, these storage
devices are usually non-volatile. In other words, the information stored on these devices is retained
even after the computer restarts following a system reboot or a power failure. A file is a collection of
related information that is stored on a secondary storage device. From the perspective of the user, a
file is the smallest unit of logical storage, i.e. data cannot be stored on a secondary storage unless it is
in the form of a file. The creator of a file defines the information within the file. A file can hold
various types of information like source programs, object programs, executable programs, sound
recordings, graphic images, payroll records, text, numeric data etc. Depending on the type of a file,
its structure can be defined. For example, a text file is a sequence of characters organized into a
number of lines or pages.

5.2 File Concept

To make it convenient for users to access a file, it is usually named with a string of characters, for
e.g. photo.jpg. Some systems do not differentiate between uppercase and lowercase characters in the
names, but others may consider this. Once a file is named, it becomes independent of the system, the
user or the process that created it. In other words, after the file named photo.jpg has been created by
one user, another user may access the file to modify it. The owner of the file may store it on a floppy
disk, send it via email, or copy it across a network. However, the file name could still remain the
same on the destination system.

File attributes

The attributes of a file vary from one operating system to another. But basically each file has the
following attributes:

Name: This is the symbolic file name which is kept in a human readable form.

Identifier: This is a unique tag which is in the form of a number. It is the only non-human
readable name for the file which identifies the file within the file system.

Type: This information is needed for systems that support different types of files.

Location: This information points to the device and the location on the device where the file
resides.

Size: This attribute denotes the current size of the file (in bytes, words, or blocks) and
possibly the maximum allowed size.

Protection: This is the access-control information which specifies who is authorized to do


what reading, writing, executing, etc.
File Management | 3

Time, data and user identification: This information may be maintained for creation, last
modification, and last use. These data can be useful for usage monitoring, security and
protection.

A directory is also maintained on secondary storage to keep the information about all the files. Each
entry in the directory consists of the file name and its unique identifier. The identifier can be used to
locate the other attributes of the file. This information may take more than 1 kilobyte of space in
main memory. Therefore, if there are many files in a system, the size of the directory itself can be in
the order of megabytes.

File operations

A file is an abstract data type. To be able to properly define a file, we should consider the different
types of operations that can be performed on files. The OS allows us to create, write, read, reposition,
delete and truncate files. Let us see how the operating system helps us in carrying out these
operations.

Creating a file: To create a file, two steps are executed one after another. The first step is to
find space for the file in the file system. The second step is to make an entry for the file in the
directory.

Writing a file: A system call is made, in which the name of the file and the information that
is to be written to the file is specified. The system looks for the location of the file in the
directory using the file name. System also keeps a write pointer to the location in the file
where the next write is to take place. Whenever a write occurs, this pointer should be
updated.

Reading a file: A system call is made in which the name of the file and where the next block
should be put is specified. System looks in the directory for the associated entry. It keeps a
read pointer to the location in the file where the next read is to take place. This pointer is
updated after the read has taken place. The same pointer is used by the read and write
operations. This saves space and reduces system complexity.

Repositioning within a file: System searches the directory for the appropriate entry and then
the current-file-position pointer is repositioned to a given value. Repositioning is only a file
seek operation. It does not involve any actual I/O operation.

Deleting a file: We first look in the directory for the given file name. After finding the
corresponding directory entry, the space allotted to the file is released so that it can be used
by other files. Finally, the directory entry is deleted.

Truncating a file: Sometimes we may want to delete the contents of a file but retain its
attributes. With this function, we do not have to delete and then recreate the file. This
function allows all attributes to remain unchanged, except for the file length. The file length
is reset to zero and the file space is released.
4 | Operating System

Besides the six operations discussed above, there are some other operations like renaming an existing
file or appending new information to the end of an existing file. A combination of the above
elementary operations can give us some other file operations. For example, a copy of a file can be
created by performing a combination of the operations- creating a new file, reading the old file and
writing to the new file.

Access Methods

Information is stored in files. When needed the system should be able to retrieve the information into
the main memory. Information in files can be accessed in many ways. Some systems allow only a
single access mode, whereas others may allow more than one access mode. A major challenge is
which access method to choose depending on the present application.

(a) Sequential Access

Sequential access is the simplest and the most common access method. Here, the information within
the file is processed in an orderly manner i.e. one record after another. Compilers and editors use this
access method to access the files. Reads and writes make up the bulk of the operations on a file. A
read operation i.e. read next, reads the next portion of the file. It advances a file pointer automatically
that tracks the location of the I/O. In the same fashion, the write operation i.e. writenext-appends to
the end of the file and advances to the end of the newly written material (the new end of file). Such a
file can be reset to the beginning. In some systems, we can use programs to skip forward or backward
n records at a time, where n can be any integer. Figure 5.1 shows how sequential access can be used
to access file records.

(b) Direct Access

Another method is the direct access method or the relative access method. A file consists of fixed
length logical records. This allows programs to read and write the records in any possible order.
Disks allow file blocks to be accessed randomly. Therefore, direct-access method is also based on a
disk model of a file. Each file is considered to be a sequence of records or blocks that are numbered.
With direct access, we can read block 5, then read block 28 and then write block 15. There is
absolutely no restriction on the order in which we perform the read or write operations. When the
information is very large, then direct access method provides a much faster access as compared to the
sequential access method. The databases of most of the systems support direct access method. As
soon as a query arrives, the block which contains the answer is computed first. Then, that block is
read directly to get the desired answer. For example, in an airline-reservation system, all the
information related to flight 724 is stored in block number 724. Hence, the number of seats that are
available for flight 724 is stored in block 724 of the reservation file. Figure 5.1 shows the basic
difference between sequential access and direct access.
File Management | 5

Figure 5.1: Sequential access vs Direct access

In case of direct-access method, block number should be included in file operations as a parameter.

Thus, instead of read nextwe have read n, where n is the block number. Similarly, instead of write
next we have write n, where n is again the block number. One alternate method is to keep read next
and write next as in sequential access, and include another operation position file to n, where n is the
block number. So to implement read n, we first position to n and then read next.

Generally, the block number that a user provides to the OS is a relative block number. A relative
block number is an index with respect to the beginning of the file. In some systems the relative block
number starts at 0, whereas in others it starts at 1. Hence, if the first relative block of the file is 0, the
next relative block is 1, and so on. The absolute disk address may however be different. It may be
12756 for the first block and 4390 for the second block. With relative block numbers, the OS can
decide where to place the file. The user is prevented from accessing those portions of the file system
that are not a part of his file.

Here it should be noted that all operating systems do not support both the access methods
sequential access and direct access. Some systems permit the sequential access only, whereas others
permit only the direct access method. In some systems, the type of access should be specified at the
time of file creation. So, whenever this file is to be accessed, only the specified access methodcan be
used. It is easy to simulate sequential access on a direct access file. Figure 5.2 shows this can be done
by using a variable cp to define the current position. It is however inefficient and clumsy to simulate
direct-access on a sequential-access file.
6 | Operating System

Figure 5.2: Simulation of sequential access on a direct-access file

(c) Other access methods

Some other access methods can also be built on top of a direct-access method. Generally, these
methods build an index for the file. This index is similar to the index that you find at the end of a
book. There are pointers that point to various blocks. The first step to find a record in the file is to
look in the index to get the pointer. The pointer is then used to access the file and find the record.
Suppose that a retail-price file lists the universal codes (UPCs) for some items along with their
associated prices. Each record in the file is 16 bytes in size and consists of a 10-digit UPC and a 6-
digit price. If the block size on the disk is 1024 bytes, then 64 records can be placed in each block.

If a file has 120,000 records, then it will occupy around 2000 blocks (2 million bytes). With the
records sortedby UPC, the first UPC in each block can be included in the index that we create for the
file. Each entry in the index will be of 10 digits. Thus, the index will occupy about 20,000 bytes and
can be kept in memory. To find the price of a specific item, a binary search is made on the index.
This search will tell accurately which block holds the desired record and that block can be accessed.
This method lets us search big files with reduced I/O operations. But as the file size increases the size
of the index also increases. The index size in some cases may be big enough to be stored in the
memory. One solution to the problem is to create an index for the index file itself. The primary index
file will contain pointers to the secondary index file, which in turn will point to the actual data items.

For instance, IBM's indexed sequential-access method (ISAM) uses a two level indexing for a file. A
master index file points to the disk blocks of a secondary index file. The secondary index in turn
points to the disk blocks that contain the actual data items. The records in the file are sorted on a
predefined key. To find a specific item, first the master index file is searched using a binary search.
When the disk block of the secondary index is found, it is then searched using a binary search to find
the block that holds the desired record. Finally, this block is searched sequentially to get the record.
Figure 5.3 shows the example of a two level indexing where every record can be located with the
help of its key.
File Management | 7

Figure 5.3: A two level indexing


8 | Operating System

5.3 Directory Structure

There is no restriction on the number of files that can be placed within a computer system. Generally,
files are stored in random-access storage devices like optical disks, hard disks etc. A storage device
can contain a single file system, or it can be partitioned to hold multiple file systems. Partitioning
helps in restricting the sizes of individual file systems, allowing the device to hole multiple file
systems, or leaving some part of the device for other uses like unformatted disk space or swap space.
We call an entity that contains a file system, a volume. A volume can be a subset of a device or
multiple devices linked together into a RAID (Redundant Array of Independent Disks)set. Volumes
can be considered as virtual disks. Volumes can store multiple operating systems to allow a system to
boot and run more than one OS. Any volume that contains a file system should also contain
information about the files in the system. The information is maintained in the form of entries into a
device directory. This directory stores information like the type, location, name and size of files on
the volume. Figure 5.4 shows how file systems are maintained on disks.

Figure 5.4: File-system organization


While considering a specific directory structure, the operations that can be performed on a directory
should also be kept in mind:

Search for a file We should be able to find in the directory structure, the entry for a
specific file. Files have symbolic names and similar names may denote that there is a
relationship among files. We should be able to find all such files whose names match a
specific pattern.

Create a file We should be able to create new files and add an entry to the directory for
each new file created.
File Management | 9

Delete a file When a file is not required anymore, we should be able to delete the file and
the corresponding entry in the directory.

List a directory We should be able to list the files in a directory and the contents of the
directory entry for each file in the list

Rename a fileA file name suggests the contents of the file. We should be able to change the
file name when the contents or the use of the file changes. Renaming a file may lead to the
change of its position in the directory structure.

Traverse the file system We may desire to access every directory and also every file within
a directory structure. From time to time we should copy the structure and the contents of the
file system to magnetic tape. This will keep the backup ready in case of system failure.

We will now discuss the common methods for defining the logical structure of a directory.

(a) Single-Level Directory

Single-level directory is the simplest directory structure. The same directory contains all the files.
This makes the structure easy to understand and work with. But when the number of files increases or
the system has more than one user, then this directory structure may not work efficiently. All the files
in the directory should have unique names. A name of a file indicates the contents of the file. But file
names are limited in length, and so finding unique names for all the files is a complex task. UNIX
allows a file name to contain 255 characters, whereas MS-DOS allows a file name to contain 11
characters only. It may be difficult for even a single user to remember the names of all the files on a
single-level directory. With increase in the number of files, it becomes even more difficult to
remember their names. A user may have a good number of files on one computer system and an
equal number of additional files on another system. To keep track of so many files is certainly not an
easy task. Figure 5.5 below shows the directory structure of a single-level directory.

Figure 5.5: Single-level directory


(b) Two-Level Directory
With a two-level directory, every user has his/her own user file directory (UFD). All the UFDs are
similar in structure but each UFD lists the files of a particular user only. When a user logs in or a user
job starts, the master file directory (MFD) is searched first. Generally, the user name or account
number of the user is used as an index into the MFD. Each entry in the MFD is used as a pointer to
10 | Operating System

the UFD of that user. When a user refers to a particular file only his own UFD is searched. Hence,
different users can have files with the same name, provided that names of all files within each UFD
are unique. When a user wants to create a file, OS looks in the UFD of that user to check if any
existing file has the same name. When a user wants to delete a file, only the corresponding UFD is
searched to find the file and delete it. Figure 5.6 shows the directory structure of a two-level
directory.

Figure 5.6: Two-level directory


We have seen that two-level directory solves the name-collision problem that was an issue in single-
level directory. However, two-level directory itself suffers from some issues. The problem arises
when a user wants to access the files of another user to cooperate on some task. Some systems do not
allow files to be accessed by other users. A two-level directory can be viewed as a tree or an inverted
tree of height 2. The root of the tree is the Master File Directory.

Its direct descendants are the User File Directories. The descendants of the UFDs are the files
themselves. A path from the root, to each file in the tree, can be defined by specifying the user name
along with the file name. For instance, if user 1 wants to access his own file named a, then he can
simply refer to a. However, if user 1 wants to access file a of user 2 then he has to specify the path
name as /user2/a.

(c) Tree-Structured Directories

With tree-structured directory a user can create his own subdirectories and organize his files
accordingly. This is the most common directory structure. There is a root directory at the top of the
tree. Each directory or subdirectory contains a set of files or subdirectories. The internal structure of
all the directories is the same. Each directory entry includes one bit 0 to represent that the entry
corresponds to a file and 1 to represent that the entry corresponds to a subdirectory. To create and
delete directories we use special system calls. Figure 5.7 shows the structure of a tree-structured
directory.
File Management | 11

Figure 5.7: Tree-structured directory

Generally, each process has a current directory. This directory containsa majority of the files that are
of current interest to the process. The current directory is searched whenever a file is referenced. If
the desired file is not found in the current directory, then the user must explicitly specify the path
name of the file or change the current directory to the directory that holds the file. If a directory is
empty, then the entry in the directory that contains it can be deleted easily. The problem arises
however, when the directory is not empty and contains some files or subdirectories. In some systems
like MS-DOS, a directory that is not empty cannot be deleted. Therefore, all the files or
subdirectories within that directory need to be deleted first in order to delete the directory. This
approach can consume a reasonable amount of time. In UNIX however, a user can delete a directory
which is not empty. This approach is convenient but risky too, as an entire directory structure will be
removed with just one command.

With a tree-structured directory system, a user can access files of other users also. For instance, user
A can access the files of user B just by specifying the correct path name. Another way of accessing
user Bs files is to change the current directory to user Bs directory and access the file by its file
name.
12 | Operating System

(d) A cyclic-Graph Directories

An acyclic-graph directory structure allows files and subdirectories to be shared among directories. In
other words, two different directories can contain the same file or subdirectory. As multiple users are
accessing the same file, therefore a change by one user can be seen by other users as well. Similarly,
if a new file is created by one user, then it will automatically appear in all the shared subdirectories.
Figure 5.8 shows the structure of acyclic-graph directories.

Figure 5.8: Acyclic-graph directory structure

There are many techniques to implement shared files and subdirectories. A basic technique is to
create a new directory entry called a link. A link is nothing but a pointer to another subdirectory or
file. When a file is referenced, the directory is searched. If the directory entry is marked as a link,
then the name of the real file is includedin the link information. Links are resolved by using that path
name to locate the real file. Another technique to implement shared files is to keep all information
about them in both the sharing directories. Acyclic-graph directories are more flexible than tree-
structured directories, but are more complex. A file may have more than one absolute path name.
Accordingly, the same file may be referred by distinct file names. Another problem involves deletion.
If the file is removed when somebody deletes it, then it may leave dangling pointers, pointing to the
file that does not exist now. Additionally, if the remaining file pointers contain actual disk addresses,
and the space is subsequently reused for other files, these dangling pointers may point into the middle
of other files.
File Management | 13

(e) General Graph Directory

Adding just files and subdirectories to a tree-structured directory preserves the tree-structured nature.
However, with the addition of links, the tree-structure is destroyed and what we get is a simple graph
structure as shown in figure 5.9. The main advantage of acyclic graph directory structure is that it
prevents us from traversing shared sections of an acyclic graph twice. This saves us from spending
extra time and effort. To avoid the same situation in general graph directory where cycles are
involved, the number of directories that will be accessed during a search can be limited.

Figure 5.9: General graph directory

Similarly, a problem arises when the decision is to be taken regarding when a file can be deleted. In
case of acyclic-graph directory structures, when the value of reference count is 0 then it means that
there are no more references to the file or directory. So, the file can be deleted. But, when cycles
exist, the value of reference count may not be 0 even when it is no longer possible to refer to a
directory or file. This irregularity results from the possibility of self-referencing (or a cycle) in
thedirectory structure. Here we need to use a garbage-collection scheme to determine when the last
reference has been deleted and the diskspace can be reallocated.

5.4 File Sharing

File sharing is a useful concept as this helps users to cooperate with each other to perform a particular
task. Cooperation ensures that the time and effort to accomplish the task is reduced. In this section we
will study the various facets of file sharing.
14 | Operating System

Multiple Users

When multiple users share an operating system, then some issues related to file protection, file
naming and file sharing may arise. If the directory structure allows files to be shared among users,
then the system should provide a proper mechanism to implement this. Either a user should be
allowed to access files of other users by default, or file access authorizations should be decided on the
basis of which file accesses may or may not be permitted. These are the issues that relate to access
control and protection which have been discussed later. For implementing sharing and protection, a
majority of the systems use the concept of file (or directory) owner (or user) and group. The owner of
a file is the user who can grant access and change attributes. He has most of the control on the file.
The group is a set of users who share the same type of access to the file. For instance, in a UNIX
system, the owner of a file can perform all operations on the file. Members of a group can execute
only a subset of the operations, and all other users can execute another subset of operations. It is the
file owner who decides what operations can be performed by the group and what operations can be
performed by other users. The IDs of the owner and the groups for a given file are stored with the
other file attributes.

When a user requests to perform an operation on a file, his ID is compared with the owner attribute.
This is done to check if the user is the owner of the file. If so, then he can perform all the operations
that are permissible to the owner of that file. Similarly, group IDs can also be compared and
permissions can be given accordingly.

3Remote File Systems

With the dawn of networking, it became possible for computer systems located in any corner of the
world to exchange information with each other. The first method that was implemented involves
sending files manually from one machine to another machine using programs like File Transfer
Protocol (FTP). The second method uses a Distributed File System (DFS) which allows the local
machines to view remote directories. The third method, World Wide Web (WWW), allows a browser
(client program) to locate, retrieve and display content on the WWW. Now we will study each of the
methods in more detail.

(a) File Transfer Protocol

FTP is a client/server protocol that is used to transfer files from one machine to another machine.
FTP can provide two types of access Authenticated access and Anonymous access. In authenticated
access, only a registered user is allowed to access the contents on the server after he has been
authenticated using his ID and password. Whereas in anonymous access, a user can access the
contents on a server without any authentication procedure. Transferring files from a client computer
to a server is termed as uploading and transferring files from a server to a client computer is called
downloading. Figure 5.10 shows how the FTP works in transferring files between a client computer
and a server.

During an FTP connection, two transmission channels are open:

A control channel for exchange of commands which stays open for the entire session.
File Management | 15

A data channel that opens and closes for exchange of data between the client and the server.

Figure 5.10: Transfer of files using FTP

(b) Distributed File System

A DFS is a client/server protocol in which a user accesses and processes data which is stored on a
server as if the data were present on his own computer. DFS makes the sharing of files and
information easier and faster on a network. Serverspermit clients to share files and store data as if
they were accessing information on their own systems. Nevertheless, data is fully controlled by the
servers, and clients are only given the access control.

Figure 5.11 shows the architecture of DFS in which four sites are connected over a communication
network. Both sites A and D have one server each. Similarly, both sites B and C have one client each.
The clients can access the servers through the communication network. As multiple clients can access
the same data concurrently, there should be a mechanism available in the server so that a client
always gets to access the most recent version of data and there is no data conflict. Generally, DFS
uses database or file replication where copies of data are distributed on a number of servers. This is
done to protect the data against access failures.
16 | Operating System

Figure 5.11: Distributed File System

(c) World Wide Web

World Wide Web (WWW) is not a synonym for the Internet. It is actually a subset of the Internet. A
web browser which is basically a client side program, is used to access pages on the Web. The
Internet is the actual network of networks where all the information resides. Things such as Internet
Relay Chat (IRC), Telnet, Internet gaming, FTP, and e-mail are all part of the Internet. They are not a
part of the World Wide Web. The Hyper-Text Transfer Protocol (HTTP) is the method used to
transfer Web pages to our computer. With hypertext, a word or phrase can contain a link to another
Web site. All Web pages are written in the hyper-text markup language (HTML), which works in
conjunction with HTTP.

5.5 File System Structure

Disks provide the bulk of secondary storage on which a file system ismaintained. This is because
they are rewritable i.e. information can be retrieved from a block, modified and then placed back on
the same block. Additionally, information can be accessed sequentially as well as randomly.
Switching from one file to another requires only moving the read-write heads and waiting for the disk
to rotate. With File Systems, data can be easily stored on disks and also can be retrieved easily as and
when needed. There are two issues however First issue is how the file system should look to the
user. This consists of defining a file and its attributes, the operations that can be performedon a file,
and the directory structure to organize files. The second issue is creation of data structures and
algorithms to map the logical file system to the secondary storage devices.

Generally, the file system is itself composed of many different levels as shown in figure 5.12. This
structure shown is an example of a layered design. The I/O control is the lowest layer which consists
File Management | 17

of interrupthandlers and device drivers for exchanging information between the disk system and the
main memory. A device driver is a program that controls a particular type of device attached to our
computer. The basic file system issues standard commands to the proper device driver for reading
and writing physical blocks on the disk. The I/O control layer also supervises caches and memory
buffers that hold data blocks, directories and file systems. The file organization module can translate
a logical block address into a physical block address if it knows the location of file and the file
allocation type that is used. The file-organization module also contains the free-space manager. The
work of the free-space manager is to track blocks that have not been allocated and provide these
blocks to the file-organization module when requested. The logical file system maintains metadata
information. Metadata comprises all of the file-system structure but not the actual data. Given a
symbolic file name, the logical file system provides the information that the file organization module
needs. The information about a file like the location of file contents, permissions and ownership is
maintained by the File Control Block (FCB).

Figure 5.12: Layered File System

5.6 File System Implementation

On a disk, a file system may contain information about directory structure, total number of blocks,
location and number of free blocks, how to boot the OS stored there and the individual files. They are
briefly described as follows:

A boot control block is basically the first block in a volume. It contains the information that a
system needs to boot an OS from that volume. This block is empty if the disk does not
contain an operating system.

A volume control block (one per volume) contains the volume or the partition details such as
number of blocks in the partition, the size of the blocks,a free-block count and free-block
pointers, and a free-FCB count and FCBpointers.
18 | Operating System

A directory structure (one per file system) is used to organize the files.

A per-file FCB contains many details about the file. It has a uniqueidentifier number that
allows it to be associated with a directory entry.

The in-memory information is utilized for file-system management as well as performance


improvement via caching. Data is loaded at mount time, updated during file-system operations and
discarded at dismount. The structures that are included are as follows:

An in-memory mount table contains information about each mounted volume.

An in-memory directory-structure cache contains the information of the directories that have
been accessed recently. (For directories at which volumes aremounted, it can contain a
pointer to the volume table.)

The system-wide open-file tableholds a copy of the file control block of each file that is open.
It also holds some other information.

The per-process open-file table holds the pointer to the corresponding entry in the system-
wide open-file table. Besides this, the per-process open-file table also contains some other
information.

Buffers hold file-system blocks when they are being written to the disk or read from the disk.

Application program calls the logical file system when a new file is to be created. The logical file
system is aware of the format of the directory structures. It allocates a new file control block to create
a new file. Alternatively, if FCBs have been created at file-system creation time, then a FCB is
allocated from the set of free FCBs. Next, the system reads the appropriate directory into memory,
updates it with the new file name and FCB, and writes it back to the disk. Figure 5.13 shows the
structure of a typical FCB.

Figure 5.13: File Control Block


File Management | 19

A file can be used for I/O after it has been created. But first of all the file must be opened. The file
name is passed to the logical file system by the open () call. First the open () system call checks the
system-wide open-file table to see if the file is being used by any other process. If so, then an entry is
created in the per-process open-file table that points to the existing system-wide open-file table. If the
file is not in use by any process i.e. the file is not open, then the directory structure is searched for the
given file name. After finding the file, the FCB is copied into a system-wide open-file table in
memory. This table not just stores the FCB but also keeps track of the number of processes that have
opened the file. Next, a per-process open-file table entry is created that points to the system-wide
open-file table entry and some other fields. The open() system call returns a pointer to the
corresponding per-process file-system table entry. All file operations are then executedthrough this
pointer.

Figure 5.14: In-memory file-system structures. (a) File open. (b) File read.
20 | Operating System

When the file is closed by a process, the corresponding entry in the per-process table is removed, and
the open count of the system-wide table is decremented. When the file is closed by all the users who
opened it,then updated metadata if any, is copied back to the disk-based directory structure, and the
system-wide open-file table entry is removed. The operating structures of a file-system
implementation are summarized in figure 5.14.

5.7 Partitions and Mounting

A disk can be partitioned into multiple volumes, or a single volume can span multiple partitions on
multiple disks. We will discuss the former layout here. Each partition can be raw or cooked. A raw
partition means that the partition contains no file system. This means that the system uses its own
format on disk and does not use a file system. A cooked partition on the other hand, means that the
partition contains a file system. We use a separate partition to store boot information. The system
does not have the file-system code loaded at boot time and so cannot interpret the file-system format.
Therefore, the partition that holds the boot information has its own format.

Usually, boot information is organized as a sequential series of blocks. It is loaded into the memory
as an image and execution of the image starts at a predefined location. The boot loader has adequate
knowledge about the file-system structure. It can find and load the kernel and start its execution. The
disk can have a number of partitions, and each partition can hold a different OS and a different file
system. The root partitionwhich contains the operating-system kernel and may be some other system
files, is mounted at the boot time.

Other volumes can be mounted automatically at boot time or can be manuallymounted later,
depending onthe OS. The operatingsystem verifies that the device contains a valid file system as part
of a successful mount operation. Actually, it asks the device driver to read the device directory and
verifies that the directoryhas the expected format. If the format is invalid, then the consistency of the
partition should be checked and it should also be corrected if possible. This can be done with or
without involving user intervention. At last, the OS notes that a file system is mounted in its in-
memory mount table. OS also notes the type of the file system. The detailsof this function depend on
the operating system.

5.8 Virtual File Systems

File system implementation consists of three major layers as shown in figure 5.15. The file-system
interfaceis the first layer. It is based on file descriptors and the open (), read(), write () and close ()
system calls. The second layer is the Virtual File System (VFS) layer that basically serves two
purposes:

It defines a VFS interface to separate the basic operations of file-system from their
implementation. Numerous implementations of the VFS interface may exist at the same time
File Management | 21

on the same machine. This allowsvarious types of file systems that have been locally
mounted, to be accessed transparently.

It allows a file to be uniquely representedwithin anetwork. The VFS is based on a file-


representation structure known as avnode. It contains a numerical designator for each unique
file within the network. The kernel keeps one vnode structure for each active directory or
file.

Hence, local files are distinguished on the basis of their file-system types. VFS also distinguishes
local files from remote files. The VFS activates operations specific to file-systems so that the local
requests can be handled.VFS also calls the NFS (Network File System) protocol procedures to handle
remote requests. File handles are created from the relevantvnodes andare passed as arguments to
these procedures. The layer implementing thefile-system type or the remote-file-system protocol is
the third layer of thearchitecture.

Figure 5.15: Schematic view of a virtual file system


22 | Operating System

5.9 Directory Implementation

The reliability, performance and efficiency of the file system is driven by the type of directory-
management and directory-allocation algorithms chosen. Let us discuss the issues involved in
selecting one of the following algorithms.

Linear List

Liner list is the simplest method to implement a directory. In this method, file names are maintained
in the form of a linear list with pointers to the data blocks. Though it is easy to write the code for this
method, the execution of this code consumes a considerable amount of time. To create a file, the
directory should be searched to ensure that no existing file has the same name. After that a new entry
can be added at the end of the directory. To delete a file, the file is first searched in the directory and
then the space allotted to the file is released. There are many alternatives to reuse the directory entry.
Firstly, we can include the entry in a list of free directory entries. Secondly, we can mark the entry
unused by assigning it a special name or adding one bit to each entry to denote whether the entry is
used or unused. There is a third alternative in which the last entry in the directory is copied into the
freed location and the length of the directory is decreased.

But one big disadvantage of this method is that a liner search is required every time we need to find a
file. Directory information is used frequently and many operating systems use a cache to store the
information of the directory which has been used most recently. If the information is available in the
cache, we call it a cache hit. In case of cache hit the information need not be reread from the disk. A
sorted list reduces the average search time by allowing a binary search. But this requirement may
complicate the process of file creation and deletion. This is so because substantial amounts of
directory information need to be moved for a sorted directory to be maintained. An advantage of the
sorted list is that a sorted directory listing can be produced without a separate sort step.

Hash Table

We can also use another data structure for a file directory known as a hash table. The directory
entries are stored in a linear list. Additionally, a hash data structure is also used. The hash table takes
a value that is computed from the file name. It returns a pointer to the file name in the linear list.
Thus the directory search time is greatly reduced. Insertion and deletion operations are also simple.
However, there should be a provision for collision (situation in which two file names hash to the
same location).

One of the major issues in using this method is that hash tables are usually of fixed size. Additionally,
hash functions are dependent on the size of the hash tables. For instance, we construct a linear-
probing hash table which contains 64 entries. The hash function converts file names into integers
from 0 to 63. To be able to insert a new entry for a file (65th entry) the size of the directory hash table
has to be increased, for example to 128 entries. So, a new hash function has to be used that can map
file names to the range 0 to 127. Besides this, we need to reorganize the current directory entries to
reflect their new hash-function values. An alternative method is to use a chained-overflow hash table.
File Management | 23

Instead of an individual value,each entry in the hash table can be a linked list. Collisioncan be
resolved by adding the new entry to the linked list. This method is likely to be much faster than a
linear search through the entire directory.

5.10 Allocation Methods

Numerous files can be stored on the same disk. The method of allocating space to files on the disk
decides how effectively the disk is utilized and how easily the files are accessed. The three basic
methods of allocating disk space are: contiguous allocation, linked allocation and indexed allocation.
We will now discuss how these methods allocate space to files and what are the issues that arise in
each of these allocation methods?

(a) Contiguous Allocation

In contiguous allocation, each file occupies a set of contiguous blocks on disk. For each file the
directory entry denotes the address of the starting block and the length of disk space allocated to the
file as shown in figure 5.16. If a file starts at block b and is n blocks in length, then it occupies blocks
b, b+1, b+2,., b+n-1. In case of sequential access, the file system remembers the disk address of
the last block that is referenced and reads the next block when it is required. In case of direct access,
to read a block i, we can directly jump to block b + i if the file starts at block b.

Figure 5.16: Contiguous allocation of disk space


24 | Operating System

The two most common techniques to select a hole from the available set of holes (to load a process
into disk) are- first fit and best fit. Both first fit and best fit perform better than worst fit in terms of
time and storage utilization. When we compare first fit and best fit, neither is best in terms of storage
utilization. However, first fit is usually faster than best fit. Nonetheless, all of these algorithms suffer
from external fragmentation. The problem arises when none of the available holes is big enough to
store the data.

One way to prevent disk space loss due to external fragmentation is to copy an entire file system onto
another disk or tape. Thus one large contiguous free space is now available on the original disk. After
allocating contiguous space from this large hole, the files that were copied to another disk can be
brought back to the original disk. The disadvantage of this method is that a considerable amount of
time is needed for the compaction procedure. One other issue is how much space should be allocated
to a file. If the space allocated is too little, then it may not be possible to extend the file size. And if
the user allocates a much bigger space than required, then the extra space is wasted.

(b) Linked Allocation

This allocation method resolves the issues in contiguous allocation. Here, each file is a linked list of
disk blocks which may be scattered anywhere on the disk. The directory contains a pointer to the first
block and the last block of the file. If we refer to figure 5.17, the directory contains a pointer to the
first block i.e. block 9 and a pointer to the last block i.e. block 25. The file contains five blocks, that
starts at block 9, continues through blocks 16, 1 and 10 and finally ends at block 25. Each block
contains a pointer to the next block.

Figure 5.17: Linked allocation of disk space


File Management | 25

A new file can be created by simply inserting a new entry into the directory. The pointer to the first
block of the file can be nil to denote that the file is empty. The size field is also set to 0. When a write
command is issued, the free-space management system finds a free block. It then writes to this block
and links this block to the end of the file. A file can be read by just reading blocks by following the
pointer from one block to another. The advantages of this allocation method is that there is no
external fragmentation. The size of a file can be extended as long as free blocks are available, and
compaction is never necessary.

There are also some disadvantages in linked allocation method. It works effectively only for the
sequential-access files. This is so because, to read the ith block of a file, we need to start at the first
block and follow the pointers one after another until we reach the i th block. Another disadvantage is
that pointers also consume space. Suppose that the size of block is 512 bytes and the size of a pointer
is 4 bytes. Then, the pointer occupies 0.78 percent of the disk space.

One solution to this problem is to combine a number of blocks into a cluster and allocate clusters
rather than blocks. Pointers will then occupy a smaller percentage of the files disk space. However,
allocation in the form of clusters can increase the internal fragmentation, as more space is wasted
when a cluster is partially full than when a block is partially full.

Another problem with linked allocation method is reliability. If a pointer is lost or damaged, then the
block may be wrongly linked into the free-space list or intoanother file. This can be resolved by using
doubly linked lists or by storing the file name and relative block number in each block. Such schemes
will however require even more overhead for each file.

File Allocation Table is a simple disk-space allocation method that is a variation on linked allocation.
It is used by OS/2 and MS-DOS operating systems. Some disk space is allotted at the beginning of
each volume to hold the table. For each disk block, the table has one entry, which is indexed by block
number. The directory entry contains the block number of the first block of the file. The table entry
indexed by that block number holdsthe block number of the next block in the file. This chain
continues until the last block is reached. The last block has a special end-of-file value as the table
entry. An unused block is indicated by a table value of 0. Figure 5.18 shows the structure of a file
allocation table. The file consists of disk blocks 217, 618 and 339.
26 | Operating System

Figure 5.18: File Allocation Table

(c) Indexed Allocation

With indexed allocation, all the pointers are brought together into one location i.e. the index block.
Each file has an index block associated with it, which basically is an array of disk-block addresses.
Each entry ipoints to block i of the file. The directory entry contains the address of the index block.
All pointers in the index block are set to nilwhen the file is created. When a block iis first written, a
block is obtained from the free-space manager and its address is put in the ith index-block entry. This
allocation method supports direct access and does not suffer from external fragmentation. This is so
because request for more space can be satisfied by any free block on the disk.

There are some issues with indexed allocation. This method suffers from wasted space. Generally,
the pointer overhead in this case is greater than the pointer overhead of linked allocation. To
understand this, we consider an example where a file occupies just one or two blocks. With linked
allocation, disk space is used by only one pointer per block. But with indexed allocation, disk space is
required for an entire index block, even though only one or two pointers are non-nil. Another issue is
what should be the size of the index block. If the index block is too big then more disk space will be
wasted. Whereas, if the index block is too small, then it may not be able to hold enough pointers for a
large file.
File Management | 27

Figure 5.19: Indexed allocation of disk space

Some mechanisms that deal with the issue of index block size have been described below.

Linked scheme

An index block is normally one disk block. Hence, it can be read and written directly by
itself. A number of index blocks can be linked together if the file is big. For instance, an
index block mayhave a small header that gives the name of the file and a set of the first 50
disk-block addresses. The last address is either nil or points to another index block.

Multilevel index

A type of linked representation where a first-level indexblock points to a set of second-level


index blocks, which in turn point to the file blocks. To access a block, the OS uses the first-
level index to find a second-level index block and then uses that block to find the desired
data block. Depending on the file size this approach can be continued to the third or fourth
level.
28 | Operating System

5.11 Disk Scheduling Algorithms

Operating systems use disk scheduling algorithms to schedule I/O requests arriving for disk. Disk
scheduling is also known as I/O scheduling. Disk scheduling is considered important due to the
following reasons:

Multiple I/O requests may arrive by different processes.But only one I/O request can be
served at a time by disk controller. Other I/O requests mustwait in waiting queue and need
to be scheduled.

Two or more requests may be far from each other. It can result in greater disk arm
movement.

Hard drives are one of the slowest parts of computer system and thus should be accessed in
an efficient manner.

There are many types of disk scheduling algorithms. But let us understand some important terms
before discussing the disk scheduling algorithms.

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 write. So the disk scheduling algorithm that gives minimum
average seek time is better.

Rotational Latency: Rotational Latency is the time taken by the desired sector of 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 number of bytes to be transferred.

Disk Access Time:

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

Figure 5.20: Disk Access Time and Disk Response Time


File Management | 29

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

Disk Scheduling Algorithms

(a) FCFS

First Come First Serve (FCFS) is the simplest of all the Disk Scheduling Algorithms. In FCFS, the
requests are addressed in the order they arrive in the disk queue.

The following are the advantages of FCFS:

No indefinite postponement

Every request gets a fair chance

The following are the disadvantages of FCFS:

May not provide the best possible service

Does not try to optimize seek time

(b) SSTF

In Shortest Seek Time First (SSTF), requests having shortest seek time are executed first. Hence, the
seek time of every request is calculated beforehand in queue and then they are scheduled according to
their calculated seek time. The request near the disk arm will get executed first. SSTF is definitely an
improvement over FCFS as it decreases the average response time and increases the throughput of
system.

The following are the advantages of SSTF:

Increase in throughput

Decrease in average response time

The following are the disadvantages of SSTF:

High variance of response time as SSTF favors only some requests

Overhead to calculate seek time in advance

Can cause starvation for a request if it has higher Seek time as compared to incoming
requests
30 | Operating System

(c) SCAN

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

SCAN has the following advantages:

Average response time

High throughput

Low variance of response time

SCAN has the following disadvantage:

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

(d) C-SCAN

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

These situations are avoided in Circular SCAN (C-SCAN) 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.

C-SCAN has the following advantage:

Provides more uniform wait time compared to SCAN

(e) LOOK

It is similar to the SCAN disk scheduling algorithm except 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.

(f) C-LOOK

As LOOK is similar to SCAN algorithm, in similar way, C-LOOK is similar to C-SCAN disk
scheduling algorithm. In C-LOOK, 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 ends last request.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the
disk.
File Management | 31

5.12 Network File System

Network File System interprets a set of interconnected workstations as a set of independent machines
with independent file systems. The objective is to allow some degree of sharing among these file
systems in a transparent way. Sharing is based on a client-server relationship. A machine can be both
a server and a client. To be able to access a remote directory in a transparent way from a local
machine, the client should first carry out a mount operation. This means that the remote directory
should be mounted over a directory of a local file system. After the completion of the mount
operation, the mounter directory replaces the subtree descending from the local directory and looks
like a fundamental subtree of the local file system. The local directory becomes the name of the root
of the newly mounted directory. To carry out the mount operation, the remote directory has to be
specified as an argument. This is not done transparently. We have to provide the host name or the
location of the remote directory. Nevertheless, once this is done, files in the remote directory can be
accessed in a totally transparent manner on the local machine.

Figure 5.21: Three independent file systems

Figure 5.21 shows three independent file systems of machines U, S1 and S2. At present, only the
local files can be accessed on each machine. After mounting S1:/usr/shared over U:/usr/local, the
effect can be seen in figure 5.22(a). The users of U view the file system as has been shown in 5.22(a).
After the mount operation is complete, users of U can access any file within directory dir1 by using
the prefix /usr /local/ dir1. The original directory /usr /local is no longer visible.Figure 5.22(b)
depicts cascading mounts. The figure shows the effect of mounting S2: /usr /dir2 over
U:/usr/local/dir1 which is already remotely mounted from S1. Users of U can access files within
directory dir2 by using the prefix /usr /local/ dir1.

The NFS specification differentiates between the services provided by the actual remote-file-access
services and a mount mechanism.Two different protocols have been specified for these services: a
protocol for remote file accesses- NFS protocol and a mount protocol. The protocols are specified as
sets of Remote Procedure Calls (RPCs).
32 | Operating System

Figure 5.22: Mounting in NFS (a) Mounts (b) Cascading mounts

The Mount Protocol

The mount protocol establishes the initial logical connection between a server and a client. A mount
operation includes the name of the server machine that stores the remote directory and the name of
the remote directory. The mount requestis mapped to the respective RPC and is sent to server
machine that is running the mount server. The server has an export list indicating the local file
systems that are exported for mounting purpose. This export list also specifies the names of the
machines that are allowed to mount those file systems. The specification can also include access
rights, such as read only. A distributed naming scheme can be used to store the mount tables and
export lists to simplify the maintenance and make the information available to the appropriate clients.

On receiving a mount request that complies with its export list, the server returns a file handle to the
client. This file handle works like a key for further accesses to files within the mounted file system.
The file handle contains all the information required by the server to distinguish each file from the
other. The server also keeps a list of client machines and their corresponding directories that are
mounted at present. Usually, the list is used for administrative purposes for example, sending
notification to all clients that the server is going down.

The NFS protocol

The NFS protocol offers a set of RPCs to perform remote file operations. Theprocedures support the
following operations:

Reading a set of directory entries

Searching for a file within a directory

Reading and writing files


File Management | 33

Accessing file attributes

Manipulating links and directories

Once a file handle has been established for the remotely mounted directory, these procedures can be
invoked. NFS servers are stateless i.e. they do not maintain information about their clients from one
access to another. As a result, for each request, a full set of arguments has to be provided. This
includes a unique file identifier and an absolute offset inside the file for the appropriate operations. A
sequence number is associated with each NFS request which helps the server to determine if any
requests are missing or if a request is a duplicate request. For the server and the clients to work
correctly, the list of clients which the server maintains, is not essential. Hence, it is also not necessary
to restore the list following a server crash. The list is treated just as a hint and may contain data that is
inconsistent.

Figure 5.23: NFS architecture

Before returning results to the clients, modified data should be committed to the servers disk. A
client can cache write blocks. However, when the client flushes the blocks to the server, it accepts
that they have reached the servers disks. All NFS data must be synchronously written by the server.
Client cant see if a server has crashed or has recovered from a crash. None of the blocks that are
managed for the clients undergo any damage. To increase the performance, nonvolatile
cache(typically battery-backed-up memory) can be used.Once the write is stored inthe nonvolatile
34 | Operating System

cache,disk controller acknowledges the disk write. These blocks remain intact even after a system
crash. Periodically, they are written from thisstable storage to the disk.

Each single NFS write procedure call is atomic and does not intermix with other write calls to the
same file. However, NFS protocol does not support concurrency-control. NFS is integrated into the
OSthru a VFS. Figure 5.23 shows the schematic diagram of the NFS architecture. Let us try to
understand how to handle an operation on an already open remoteFile. The client initiates
theoperation with a regular system call. The OS layer maps thiscall to a VFS operation on the
appropriate v node. The VFS layer recognizes thefile as a remote one and invokes the appropriate
NFS procedure. An RPC callis made to the NFS service layer at the remote server. This call is
reinjected tothe VFS layer on the remote system, which finds that it is local and invokesthe
appropriate file-system operation. This path is retraced to return the result. Abenefit of this
architecture is that the server and the client are identical. Hence, a machine may be a server or a
client, or even both. The actual service on eachserver is performed by kernel threads.

5.13 I/O Systems

Management of I/O devices is a major task of the operating system. These devices include
keyboards, mouse, touch pad, display adapters, disk drives, USB devices, LED, Analog-to-digital
converter, Bit-mapped screen, On/off switch, audio I/O, network connections and printers etc.An I/O
system is required to take an application I/O request and send it to the physical device. It should then
take whatever response comes back from the device and send it to the application. Basically, I/O
devices can be of the following two categories:

Block devices These are the devices with which the driver communicates by sending entire
blocks of data. For instance,USB cameras, Hard disks and Disk-On-Key etc.

Character devices These are the devices with which the driver communicates by sending
and receiving single characters (bytes/octets). For instance, parallel ports, serial ports and
sounds cards etc.

Device Controllers

A device driver is a software module that can be plugged into an operating system to manage a
specific device. OS requires the help of device drivers to manage all I/O devices.The Device
Controller actually works as an interface between a device driver and the corresponding device. I/O
units normallycontain a mechanical component and an electronic component. The electronic
component is called the device controller.

For each device there is a device driver and a device controller to communicate with the OS. A
device controller can handle multiple devices. The main task of a device controller is to convert
serial bit stream to block of bytes and to perform error correction as required.

Any device which is connected to the computer is connected by a plug and socket. The socket in turn
is connected to a device controller. Figure 5.24is a model for connecting the CPU, memory,
File Management | 35

controllers, and I/O devices. As can be seen in the figure a common bus has been used for
communication between the CPU and the device controllers.

Figure 5.24: Communication between CPU and device controllers


There can be two types of I/O as discussed below:
Synchronous I/O: In this scheme, CPU execution waits while I/O proceeds.

Asynchronous I/O: In this scheme, I/O proceeds concurrently with CPU execution.

Communication to I/O devices


The CPU has to pass information to and from an I/O device. There are three ways in which the CPU
can communicate with the I/O devices.

Special Instruction I/O

Memory-mapped I/O

Direct memory access (DMA)

(a) Special Instruction I/O

In this technique those CPU instructions are used which are specifically made for controlling I/O
devices. These instructions allow data to be sent to an I/O device or read from an I/O device.

(b) Memory-mapped I/O

With memory-mapped I/O, memory and I/O devices share the same address space. The device is
directly connected to specific locations in main memory. Thus, an I/O device can send a block of data
to memory and receive a block of data from memory without going through CPU.
36 | Operating System

Figure 5.25: Memory-mapped I/O


In memory mapped I/O, operating systemassigns buffer in memory and notifies I/O device to utilize
that buffer to send data to the CPU. I/O device operates asynchronously with CPU, interrupts CPU
when finished.The advantage of using memory mapped I/O is that every instruction which can
access memory can be used to manipulate an I/O device. Generally, memory mapped IO is used for
high-speed I/O devices like disks.

(c) Direct Memory Access (DMA)

Direct memory access (DMA) is a method that allows an input/output (I/O) device to send or receive
data directly to or from the main memory, bypassing the CPU to speed up memory operations. The
process is managed by a chip known as a DMA controller (DMAC).

A computer's system resource tools are used for communication between hardware and software.
The four types of system resources are:

1. I/O addresses
2. Memory addresses
3. Interrupt request numbers (IRQ)
4. Direct memory access (DMA) channels

DMA channels are used to communicate data between the peripheral device and the system memory.
All four system resources rely on certain lines on a bus.

Slow devices such as keyboards generate an interrupt to the main CPU after each byte has been
transferred. If a fast device like a disk generated an interrupt for each byte, then the OS would spend
a lot of time handling these interrupts. Thus, typically computers use direct memory access (DMA)
hardware to reduce this overhead.
File Management | 37

With DMA, CPU grants I/O module authority to read from or write to memory without being
involved itself. The DMA module itself controls exchange of data between the I/O device and main
memory. The CPU is involved only at the beginning and the end of the transfer and is interrupted
only after entire block has been transferred.

A special hardware called DMA controller (DMAC) is needed. It manages the data transfers and
arbitrates access to the system bus. The controllers are programmed with source and destination
pointers (where to read/write the data), settings, which includes I/O and memory types, interrupts
and states for the CPU cycles and counters to track the number of transferred bytes.

Figure 5.26: Direct Memory Access

In general, following is the sequence of operations performed by a DMA

Initially, when any device has to send data between the device and the memory, the device
has to send DMA request (DRQ) to DMA controller.

The DMA controller sends Hold request (HRQ) to the CPU and waits for the CPU to assert
the HLDA (Hold Acknowledge).

Then the microprocessor tri-states all the data bus, address bus, and control bus. The CPU
leaves the control over bus and acknowledges the HOLD request through HLDA signal.

Now the CPU is in HOLD state and the DMA controller has to manage the operations over
buses between the CPU, memory, and I/O devices.

The operating system uses the DMA hardware in the following way to transfer data from Disk:
38 | Operating System

1. Device driver is instructed to transfer disk data to a buffer address X.

2. Device driver then instructs disk controller to transfer data to buffer.

3. Disk controller starts DMA transfer.

4. Disk controller sends each byte to DMA controller.

5. DMA controller transfers bytes to buffer, increases the memory address, decreases the
counter C until C becomes zero.

6. When C becomes zero, DMA interrupts CPU to signal transfer completion.

Polling vs Interrupts I/O


There must be a way in which a computer can detect the arrival of any type of input. There are two
ways in which this can happen - polling and interrupts. Both of these methods allow the processor
to deal with events that can happen at any time and that are not related to the process it is currently
running.

(a) Polling I/O

Polling is the simplest way for an I/O device to communicate with the processor. In this method, the
status of a device is periodically checked to see if it is time for the next I/O operation. The I/O
device puts the information in a Status register. The processor must come and get the information.

Often, the devices will not require attention and when one does it will have to wait until it is next
interrogated by the polling program. This is an inefficient method and much of the processors time
is wasted on unnecessary polls.

(b) Interrupts I/O

An alternative scheme for dealing with I/O is the interrupt-driven method. An interrupt is a signal to
the microprocessor from a device that requires attention.

A device controller puts an interrupt signal on the bus when it needs CPUs attention when CPU
receives an interrupt. It saves its current state and invokes the appropriate interrupt handler using the
interrupt vector (addresses of OS routines to handle various events). When the interrupting device
has been dealt with, the CPU continues with its original task as if it had never been interrupted.

5.14 Summary

There are basically two types of file access methods Sequential access and Direct access. In
sequential access the information within the file is processed in an orderly manner i.e. one record
after another. In Direct access method we can immediately jump to any other block to read its
contents no matter in which block we are. There is another access method in which an index is built
for a file. The index contains pointers to various blocks. A directory can have any of the following
logical structures- single-level directory, two-level directory, tree-structured directory, acyclic-graph
directory and general-graph directory. FTP is a client/server protocol that is used to transfer files
File Management | 39

from one machine to another machine. A DFS is a client/server protocol in which a user accesses and
processes data which is stored on a server as if the data were present on his own computer. In WWW,
a browser which is a client program is used to access pages on the Web. Numerous files can be stored
on the same disk. In contiguous allocation, each file occupies a set of contiguous blocks on disk. In
linked allocation, each file is a linked list of disk blocks which may be scattered anywhere on the
disk. With indexed allocation, all the pointers are brought together into one location i.e. the index
block. Network File System interprets a set of interconnected workstations as a set of independent
machines with independent file systems. The objective is to allow some degree of sharing among
these file systems in a transparent way.
40 | Operating System

5.15 Exercise

1. A ......................... is collection of related fields that can be treated as a unit by some application
program.

A) field B) record C) file D) database

2. .......................... communicate directly with peripheral devices or their controllers or channels.

A) Basic I/O supervisor B) Physical I/O

C) Device drivers D) Logical I/O

3. In .......................... file organization, a fixed format is used for records where all records are of
the same length, consisting of the same number of fixed length fields in a particular order.

A) pile B) sequential

C) indexed sequential D) indexed

4. The ........................... maintains the key characteristic of the sequential file: Records are
organized in sequence based on a key field.

A) pile B) sequential file

C) indexed sequential file D) indexed file

5. ....................... is a pre allocation strategy, using variable size portions where the file allocation
table needs just a single entry for each file, showing the starting block and the length of the file.

A) Chained allocation B) Indexed allocation

C) Contiguous allocation D) Variable allocation

6. Typically, ..................... is on an individual block basis where each block contains a pointer to
the next block in the chain.

A) Contiguous allocation B) Indexed allocation

C) Chained allocation D) Variable allocation

7. Which of the following is/are the types of operations that may be performed on the directory.

i) Search ii) Create file iii) Create directory iv) List directory

A) i, ii and iii only B) ii, iii and iv only

C) i, ii and iv only D) All i, ii, iii and iv

8. The time taken to move the disk arm to the desired cylinder is called the:
File Management | 41

A) positioning time B) random access time

C) seek time D) rotational latency

9. When the head damages the magnetic surface, it is known as _________.

A) disk crash B) head crash C) magnetic damage D) All of these

10. In which of the following access methods memory and I/O devices share the same address
space?

A) DMA B) Memory mapped I/O

C) Special Instruction I/O D) None of these

11. What are the requirements of a file management system? Write down the basic functions of a
file management system in OS.

12. Compare and contrast between the two file access methods- Sequential access and Direct
access.

13. What are the operations that should be kept in mind while choosing the directory structure for
the file system? What are the advantages of maintaining a two-level directory structure over a
single-level directory structure?

14. With the help of a neat and labelled diagram explain how files are allocated space on a disk in
the linked allocation method. Suggest at least two ways in which it outperforms the contiguous
allocation method.

15. Define the following terms:

a) Seek Time b) Rotational Latency

c) Transfer Time d) Disk Access Time

e) Disk Response Time

16. How does the SSTF disk scheduling work? What are its advantages and disadvantages?

17. Draw the architecture of Network File System. Explain in detail how the NFS protocol helps in
performing remote file operations?

18. Write down and explain the steps in a DMA access. Draw a diagram to support your answer.

19. What file allocation strategy is most appropriate for random access files?Justify your statement.

20. Give an example where contiguous allocation of file blocks on disks can be used in practice.
42 | Operating System

5.16 Suggested Readings

1. Operating System Concepts by Abraham Silberschatz, Peter B. Galvin, Greg Gagne,Wiley.

2. Operating Systems: Internals and Design Principles by William Stallings, Pearson.

Вам также может понравиться