OS UNIT 5 NOTES
OS UNIT 5 NOTES
The I/O management subsystem controls all the input and output of the computer system.
For the enforcement of security, the most important things that the I/O management subsystem does are.
managing the transfer of data.
Enforcing access controls (the DAC mechanisms) on data while it is being transferred.
Device management in an operating system means controlling the Input/Output devices like disk,
microphone, keyboard, printer, magnetic tape, USB ports, camcorder, scanner, other accessories, and
supporting units like supporting units control channels.
A process may require various resources, including main memory, file access, and access to disk drives, and
others.
If resources are available, they could be allocated, and control returned to the CPU.
Otherwise, the procedure would have to be postponed until adequate resources become available.
The system has multiple devices, and in order to handle these physical or virtual devices,
the operating system requires a separate program known as an ad device controller.
It also determines whether the requested device is available.
Boot Device
It stores data in fixed-size blocks, each with its unique address.
For example- Disks.
Character Device
It transmits or accepts a stream of characters, none of which can be addressed individually.
For instance, keyboards, printers, etc.
Network Device
It is used for transmitting the data packets.
Functions of the device management in the operating system.
The operating system (OS) handles communication with the devices via their drivers.
The OS component gives a uniform interface for accessing devices with various physical features.
There are various functions of device management in the operating system. Some of them are as follows:
1
It keeps track of data, status, location, uses, etc. The file system is a term used to define a group of
facilities.
It enforces the pre-determined policies and decides which process receives the device when and for how
long.
It improves the performance of specific devices.
It monitors the status of every device, including printers, storage drivers, and other devices.
It allocates and effectively deallocates the device.
De-allocating differentiates the devices at two levels: first, when an I/O command is issued and
temporarily freed.
Second, when the job is completed, and the device is permanently release.
Types of devices
There are three types of Operating system peripheral devices: dedicated, shared, and virtual. These are as
follows:
1. Dedicated Device
In device management, some devices are allocated or assigned to only one task at a time until that
job releases them.
Devices such as plotters, printers, tape drivers, and other similar devices necessitate such an
allocation mechanism because it will be inconvenient if multiple people share them simultaneously.
The disadvantage of such devices is the inefficiency caused by allocating the device to a single user
for the whole duration of task execution, even if the device is not used 100% of the time.
2. Shared Devices
These devices could be assigned to a variety of processes.
By interleaving their requests, disk-DASD could be shared by multiple processes simultaneously.
The Device Manager carefully controls the interleaving, and pre-determined policies must resolve
all difficulties.
3. Virtual Devices
Virtual devices are a hybrid of the two devices, and they are dedicated devices that have been
transformed into shared devices.
For example, a printer can be transformed into a shareable device by using a spooling program that
redirects all print requests to a disk.
A print job is not sent directly to the printer; however, it is routed to the disk until it is fully prepared
with all of the required sequences and formatting, at which point it is transmitted to the printers.
The approach can transform a single printer into numerous virtual printers, improving performance
and ease of use.
The process of temporarily storing data that is passing between a processor and a peripheral.
The usual purpose is to smooth out the difference in rates at which the two devices can handle data.
A buffer is a memory area that stores data being transferred between two devices or between a device
and an application.
2
Uses of I/O Buffering:
Buffering is done to deal effectively with a speed mismatch between the producer and consumer of the
data stream.
A buffer is produced in main memory to heap up the bytes received from modem.
After receiving the data in the buffer, the data get transferred to disk from buffer in a single operation.
This process of data transfer is not instantaneous; therefore the modem needs another buffer in order to
store additional incoming data.
When the first buffer got filled, then it is requested to transfer the data to disk.
The modem then starts filling the additional incoming data in the second buffer while the data in the
first buffer getting transferred to disk.
When both the buffers completed their tasks, then the modem switches back to the first buffer while the
data from the second buffer get transferred to the disk.
The use of two buffers disintegrates the producer and the consumer of the data, thus minimizes the time
requirements between them.
Buffering also provides variations for devices that have different data transfer sizes.
Types of various I/O buffering techniques:
1. Single buffer:
A buffer is provided by the operating system to the system portion of the main memory.
2. Double buffer:
3
a) Block oriented –
There are two buffers in the system.
One buffer is used by the driver or controller to store data while waiting for it to be taken by higher
level of the hierarchy.
Other buffer is used to store data from the lower-level module.
Double buffering is also known as buffer swapping.
A major disadvantage of double buffering is that the complexity of the process get increased.
If the process performs rapid bursts of I/O, then using double buffering may be deficient.
b) Stream oriented –
Line- at a time I/O, the user process need not be suspended for input or output, unless process runs
ahead of the double buffer.
Byte- at a time operations, double buffer offers no advantage over a single buffer of twice the
length.
1.FCFS:
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.
Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is: 50
So, total seek time:
= (82-50) +(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16)
=642
Advantages:
1. Every request gets a fair chance
5
2. No indefinite postponement
Disadvantages:
1. Does not try to optimize seek time
2. May not provide the best possible service
2. SSTF:
In SSTF (Shortest Seek Time First), requests having shortest seek time are executed first.
So, the seek time of every request is calculated in advance in the queue and then they are scheduled
according to their calculated seek time.
As a result, the request near the disk arm will get executed first.
SSTF is certainly an improvement over FCFS as it decreases the average response time and increases the
throughput of system.
Let us understand this with the help of an example.
Example:
Suppose the order of request is- (82,170,43,140,24,16,190)
And current position of Read/Write head is : 50
=(50-43)+(43-24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170)
=208
Advantages:
Disadvantages:
1. Overhead to calculate seek time in advance
2. Can cause Starvation for a request if it has higher seek time as compared to incoming requests
3. High variance of response time as SSTF favours only some requests
6
3. 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 as 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.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and
it is also given that the disk arm should move “towards the larger value”.
=(199-50)+(199-16)
=332
Advantages:
1. High throughput
2. Low variance of response time
3. Average response time
Disadvantages:
1. Long waiting time for requests for locations just visited by disk arm.
4. CSCAN:
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 CSCAN algorithm in which the disk arm instead of reversing its direction
goes to the other end of the disk and starts servicing the requests from there.
7
So, the disk arm moves in a circular fashion and this algorithm is also similar to SCAN algorithm and
hence it is known as C-SCAN (Circular SCAN).
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50, and
it is also given that the disk arm should move “towards the larger value”.
=(199-50)+(199-0)+(43-0)
=391
Advantages:
1. Provides more uniform wait time compared to SCAN.
5. LOOK:
It is similar to the SCAN disk scheduling algorithm except for the difference that the disk arm in spite
of going to the end of the disk goes only to the last request to be serviced in front of the head and then
reverses its direction from there only.
Thus, it prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50,
and it is also given that the disk arm should move “towards the larger value”.
=(190-50)+(190-16)
=314
6. CLOOK:
8
As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to CSCAN disk
scheduling algorithm.
In CLOOK, the disk arm in spite of going to the end goes only to the last request to be serviced in front
of the head and then from there goes to the other end’s last request.
Thus, it also prevents the extra delay which occurred due to unnecessary traversal to the end of the disk.
Example:
Suppose the requests to be addressed are-82,170,43,140,24,16,190. And the Read/Write arm is at 50,
and it is also given that the disk arm should move “towards the larger value”
=(190-50)+(190-16)+(43-16)
=341
7. RSS–
It stands for random scheduling and just like its name it is nature.
It is used in situations where scheduling involves random attributes such as random processing time,
random due dates, random weights, and stochastic machine breakdowns this algorithm sits perfect.
Which is why it is usually used for and analysis and simulation.
LIFO– In LIFO (Last In, First Out) algorithm, newest jobs are serviced before the existing ones i.e. in
order of requests that get serviced the job that is newest or last entered is serviced first and then the rest
in the same order.
Advantages
8. N-STEP SCAN –
It is also known as N-STEP LOOK algorithm. In this a buffer is created for N requests. All requests
belonging to a buffer will be serviced in one go.
Also once the buffer is full no new requests are kept in this buffer and are sent to another one.
Now, when these N requests are serviced, the time comes for another top N requests and this way all
get requests get a guaranteed service.
Advantages
1. It eliminates starvation of requests completely
9.FSCAN–
This algorithm uses two sub-queues.
During the scan all requests in the first queue are serviced and the new incoming requests are added to
the second queue.
9
All new requests are kept on halt until the existing requests in the first queue are serviced.
Advantages
1. FSCAN along with N-Step-SCAN prevents “arm stickiness” (phenomena in I/O scheduling where
the scheduling algorithm continues to service requests at or near the current sector and thus prevents
any seeking)
2.Each algorithm is unique in its own way. Overall Performance depends on the number and type of
requests.
The components of the file management system in the operating system are as follows:
1. File Attribute
2. File Operations
3. File Access Permissions
4. File Systems
File Attribute
It specifies file characteristics such as type, date of last modification, size, location on disk, and so
on.
File attributes assist the user in comprehending the value and location of files. It is used to
describe all of the information about a specific file.
File Operations
It defines the actions that can be taken on a file, such as opening and closing it.
File Systems
It defines the logical method of storing files in a computer system.
FAT and NTFS are two of the most commonly used file systems.
The various operation on files that can be performed by a user and is facilitated by the file
management in the operating system are:
1. Creating a File
2. Reading a File
3. Writing a File
4. Deleting a File
5. Truncating a File
Creating a File
In order to create a new file, there should be ample space in the file system.
The new file's directory entry must then be created. This entry should include information about
the file, such as its name, location, etc.
Reading a File
The system call must specify the file's name and location to read from a file.
A read pointer should be present at the location where the read should occur.
The read pointer should be updated once the read process is completed.
Writing a File
To write to a file, the system call should specify the file's name as well as the contents to be
written.
A write pointer should be present at the location where the write should occur.
The write pointer should be updated after the write process is completed.
Deleting a File
11
When the file is deleted, all the file space is deleted so it can be reused by other files.
Truncating a File
This removes the data from the file without erasing all of its attributes.
Only the file length is reset to zero, and the contents of the file are erased.
The remaining characteristics remain unchanged.
1. 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, 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- read the next position of the file and automatically advance a file pointer,
which keeps track I/O location.
Similarly, for the -write next- append to the end of the file and advance to the newly written material.
2.Direct Access –
12
Another method is direct access method also known as relative access method.
A filed-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.
1. The files can be immediately accessed decreasing the average access time.
2. In the direct access method, in order to access a block, there is no need of traversing all the blocks
present before it.
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.
13
A Directory is the collection of the correlated files on the disk. In simple words, a directory is like a
container which contains file and folder.
In a directory, we can store the complete file attributes or some attributes of the file.
A directory can be comprised of various files.
With the help of the directory, we can maintain the information related to the files.
There are several logical structures of a directory, these are given below.
1. Single-level directory –
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 searches whenever a new user id=s logged in.
14
The MFD is indexed by username or account number, and each entry points to the UFD for that
user.
Advantages:
1.We can give full path like /User-name/directory-name/.
2.Different users can have the same directory as well as the file name.
3. Searching of files becomes easier due to pathname and user-grouping.
Disadvantages:
1. A user is not allowed to share files with other users.
2. Still, it not very scalable, two files of the same type cannot be grouped together in the same user.
3. Tree-structured directory –
Once we have seen a two-level directory as a tree of height 2, the natural generalization is to extend
the directory structure to a tree of arbitrary height.
This generalization allows the user to create their own subdirectories and to organize their files
accordingly.
A tree structure is the most common directory structure. The tree has a root directory, and every file
in the system has a unique path.
Advantages:
1. Very general, since full pathname can be given.
2. Very scalable, the probability of name collision is less.
3. Searching becomes very easy, we can use both absolute paths as well as relative.
Disadvantages:
1. Every file does not fit into the hierarchical model, files may be saved into multiple directories.
2. We can not share files.
3. It is inefficient, because accessing a file may go under multiple directories.
15
An acyclic graph is a graph with no cycle and allows us to share subdirectories and files.
The same file or subdirectories may be in two different directories.
It is a natural generalization of the tree-structured directory.
It is used in the situation like when two programmers are working on a joint project and they
need to access files.
The associated files are stored in a subdirectory, separating them from other projects and files of
other programmers since they are working on a joint project so they want the subdirectories to
be into their own directories.
The common subdirectories should be shared. So here we use Acyclic directories.
It is the point to note that the shared file is not the same as the copy file.
If any programmer makes some changes in the subdirectory it will reflect in both subdirectories.
Advantages:
1. We can share files.
2. Searching is easy due to different-different paths.
Disadvantages:
1. We share the files via linking, in case deleting it may create the problem,
2. If the link is a soft link then after deleting the file we left with a dangling pointer.
3. In the case of a hard link, to delete a file we have to delete all the references associated with
it.
5. General graph directory structure –
In general graph directory structure, cycles are allowed within a directory structure where
multiple directories can be derived from more than one parent directory.
The main problem with this kind of directory structure is to calculate the total size or space that
has been taken by the files and directories.
Advantages:
16
1. It allows cycles.
2. It is more flexible than other directories structure.
Disadvantages:
1. It is more costly than others.
2. It needs garbage collection.
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.
17
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:
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.
Device drivers acts as interface between devices and OS, they help to transfer data between disk and
main memory.
18
It takes block number as input and as output it gives low level hardware specific instruction.
Basic file system –
It Issues general commands to device driver to read and write physical blocks on disk.
It manages the memory buffers and caches.
A block in buffer can hold the contents of the disk block and cache stores frequently used
file system metadata.
Advantages:
1. Duplication of code is minimized.
2. Each file system can have its own logical file system.
Disadvantages:
1. If we access many files at same time then it results in low performance.
2. We can implement file system by using two types data structures :
1. On-disk Structures –
Generally, they contain information about total number of disk blocks, free disk blocks, location
of them and etc.
Below given are different on-disk structures:
Boot Control Block –
It is usually the first block of volume, and it contains information needed to boot
an operating system.
In UNIX it is called boot block and in NTFS it is called as partition boot sector.
Volume Control Block –
It has information about a particular partition ex: - free block count, block size and block pointers
etc.
In UNIX it is called super block and in NTFS it is stored in master file table.
Directory Structure –
They store file names and associated inode numbers.
In UNIX, includes file names and associated file names and in NTFS, it is stored in master file
table.
Per-File FCB –
It contains details about files, and it has a unique identifier number to allow association with
directory entry.
19
In NTFS it is stored in master file table.
2. In-Memory Structure:
They are maintained in main-memory, and these are helpful for file system management for
caching. Several in-memory structures given below :
1. Mount Table –
It contains information about each mounted volume.
2. Directory-Structure cache –
This cache holds the directory information of recently accessed directories.
3. System wide open-file table –
It contains the copy of FCB of each open file.
4. Per-process open-file table –
It contains information opened by that particular process and it maps with appropriate system wide
open-file.
1. Linear List –
2. Hash Table –
20
The hash table takes a value computed from the file name and returns a pointer to the file.
It decreases the directory search time.
The insertion and deletion process of files is easy.
The major difficulty is hash tables are its generally fixed size and hash tables are dependent on
hash function on that size.
The allocation methods define how the files are stored in the disk blocks.
There are three main disk space or file allocation methods.
1. Contiguous Allocation
2. Linked Allocation
3. Indexed Allocation
All the three methods have their own advantages and disadvantages as discussed below:
1. Contiguous Allocation
In this scheme, each file occupies a contiguous set of blocks on the disk.
For example, if a file requires n blocks and is given a block b as the starting location, then the
blocks assigned to the file will be: b, b+1, b+2,……b+n-1.
21
This means that given the starting block address and the length of the file (in terms of blocks
required), we can determine the blocks occupied by the file.
The directory entry for a file with contiguous allocation contains:
Advantages:
Both the Sequential and Direct Accesses are supported by this.
For direct access, the address of the kth block of the file which starts at block b can easily be
obtained as (b+k).
This is extremely fast since the number of seeks are minimal because of contiguous allocation of
file blocks.
Disadvantages:
This method suffers from both internal and external fragmentation. This makes it inefficient in
terms of memory utilization.
Increasing file size is difficult because it depends on the availability of contiguous memory at a
particular instance.
In this scheme, each file is a linked list of disk blocks which need not be contiguous.
The disk blocks can be scattered anywhere on the disk.
The directory entry contains a pointer to the starting and the ending file block. Each block contains
a pointer to the next block occupied by the file.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last block
(25) contains -1 indicating a null pointer and does not point to any other block.
Advantages:
This is very flexible in terms of file size.
File size can be increased easily since the system does not have to look for a contiguous chunk of
memory.
22
This method does not suffer from external fragmentation. This makes it relatively better in terms
of memory utilization.
Disadvantages:
Because the file blocks are distributed randomly on the disk, a large number of seeks are needed to
access every block individually.
This makes linked allocation slower.
It does not support random or direct access.
We cannot directly access the blocks of a file.
A block k of a file can be accessed by traversing k blocks sequentially (sequential access ) from
the starting block of the file via block pointers.
Pointers required in the linked allocation incur some extra overhead.
3. Indexed Allocation
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file.
Advantages:
This supports direct access to the blocks occupied by the file and therefore provides fast access to
the file blocks.
It overcomes the problem of external fragmentation.
Disadvantages:
The pointer overhead for indexed allocation is greater than linked allocation.
For very small files, say files that expand only 2-3 blocks, the indexed allocation would keep one
entire block (index block) for the pointers which is inefficient in terms of memory utilization.
23
However, in linked allocation we lose the space of only 1 pointer per block.
For files that are very large, single index block may not be able to hold all the pointers.
Following mechanisms can be used to resolve this:
A) Linked scheme:
This scheme links two or more index blocks together for holding the pointers.
Every index block would then contain a pointer or the address to the next index block.
B) Multilevel index:
In this policy, a first level index block is used to point to the second level index blocks which in
turn points to the disk blocks occupied by the file.
This can be extended to 3 or more levels depending on the maximum file size.
C) Combined Scheme:
In this scheme, a special block called the Inode (information Node) contains all the information
about the file such as the name, size, authority, etc and the remaining space of Inode is used to
store the Disk Block addresses.
The first few of these pointers in Inode point to the direct blocks i.e the pointers contain the
addresses of the disk blocks that contain data of the file.
The next few pointers point to indirect blocks. Indirect blocks may be single indirect, double
indirect or triple indirect.
Single Indirect block is the disk block that does not contain the file data but the disk address of the
blocks that contain the file data.
Similarly, double indirect blocks do not contain the file data but the disk address of the blocks that
contain the address of the blocks containing the file data.
There is a system software in an operating system that manipulates and keeps track of free spaces
to allocate and de-allocate memory blocks to files, this system is called a "file
management system in an operating system".
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:
24
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 allocated and 1 indicates a free
block.
The given instance of disk blocks on the disk in Figure 1 (where green blocks are allocated) can be
represented by a bitmap of 16 bits as: 0000111000000110.
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.
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 disk and is also
cached in memory.
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.
2. 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.
3. Counting –
25
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
26