Unit 5. File and Input_output Management
Unit 5. File and Input_output Management
File structure
A File Structure needs to be predefined format in such a way that an operating system understands. It
has an exclusively defined structure, which is based on its type.
Three types of files structure in OS:
• A text file: It is a series of characters that is organized in lines.
• An object file: It is a series of bytes that is organized into blocks.
• A source file: It is a series of functions and processes.
File Attributes
A file has a name and data. Moreover, it also stores meta information like file creation date and time,
current size, last modified date, etc. All this information is called the attributes of a file system.
Here, are some important File attributes used in OS:
• Name: It is the only information stored in a human-readable form.
• Identifier: Every file is identified by a unique tag number within a file system known as an
identifier.
• Location: Points to file location on device.
• Type: This attribute is required for systems that support various types of files.
• Size: Attribute used to display the current file size.
• Protection: This attribute assigns and controls the access rights of reading, writing, and
executing the file.
• Time, date and security: It is used for protection, security, and also used for monitoring
File Type
It refers to the ability of the operating system to differentiate various types of files like text files, binary,
and source files. However, Operating systems like MS_DOS and UNIX has the following type of files:
Character Special File
It is a hardware file that reads or writes data character by character, like mouse, printer, and more.
Ordinary files
• These types of files stores user information.
• It may be text, executable programs, and databases.
• It allows the user to perform operations like add, delete, and modify.
Directory Files
Directory contains files and other related information about those files. Its basically a folder to hold and
organize multiple files.
Special Files
These files are also called device files. It represents physical devices like printers, disks, networks,
flash drive, etc.
Functions of File
• Create file, find space on disk, and make an entry in the directory.
• Write to file, requires positioning within the file
• Read from file involves positioning within the file
• Delete directory entry, regain disk space.
• Reposition: move read/write position.
Basic File system – It Issues general commands to the device driver to read and write physical blocks
on disk. It manages the memory buffers and caches. A block in the buffer can hold the contents of the
disk block and the cache stores frequently used file system metadata.
I/O Control level – Device drivers act as an interface between devices and OS, they help to transfer
data between disk and main memory. It takes block number as input and as output, it gives low-level
hardware-specific instruction.
Devices Layer: The bottom most layer, consisting of the actual hardware devices. It performs the
actual reading and writing of data to the physical storage medium. This includes hard drives, SSDs,
optical disks, and other storage devices.
Advantages
• There is no external fragmentation with linked allocation.
• Any free block can be utilized in order to satisfy the file block requests.
• File can continue to grow as long as the free blocks are available.
• Directory entry will only contain the starting block address.
Disadvantages
• Random Access is not provided.
• Pointers require some space in the disk blocks.
• Any of the pointers in the linked list must not be broken otherwise the file will get corrupted.
• Need to traverse each block.
4. Inodes
In UNIX based operating systems, each file is indexed by an Inode. Inode are the special disk block
which is created with the creation of the file system. The number of files or directories in a file system
depends on the number of Inodes in the file system.
• A number of direct blocks which contains the pointers to first 12 blocks of the file.
• A single indirect pointer which points to an index block. If the file cannot be indexed entirely by
the direct blocks then the single indirect pointer is used.
• A double indirect pointer which points to a disk block that is a collection of the pointers to the
disk blocks which are index blocks. Double index pointer is used if the file is too big to be
indexed entirely by the direct blocks as well as the single indirect pointer.
• A triple index pointer that points to a disk block that is a collection of pointers. Each of the
pointers is separately pointing to a disk block which also contains a collection of pointers which
are separately pointing to an index block that contains the pointers to the file blocks
Device Controllers
Device drivers are software modules that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices.
The Device Controller works like an interface between a device and a device driver. I/O units
(Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic
component where electronic component is called the device controller.
Memory-mapped I/O
When using memory-mapped I/O, the same address space is shared by memory and I/O devices. The
device is connected directly to certain main memory locations so that I/O device can transfer block of
data to/from memory without going through CPU.
The advantage to this method is that every instruction which can access memory can be used to
manipulate an I/O device. Memory mapped IO is used for most high-speed I/O devices like disks,
communication interfaces.
Device Drivers
Device drivers are software modules that can be plugged into an OS to handle a particular device.
Operating System takes help from device drivers to handle all I/O devices. A device driver performs the
following jobs −
• To accept request from the device independent software above to it.
• Interact with the device controller to take and give I/O and perform required error handling
• Making sure that the request is executed successfully
Interrupt handlers
An interrupt handler, also known as an interrupt service routine or ISR, is a piece of software or more
specifically a callback function in an operating system or more specifically in a device driver, whose
execution is triggered by the reception of an interrupt.
When the interrupt happens, the interrupt procedure does whatever it has to in order to handle the
interrupt, updates data structures and wakes up process that was waiting for an interrupt to happen.
1. Low-level Formatting
Low level formatting is a type of physical formatting. It is the process of marking of cylinders and
tracks of the blank hard-disk. After this there is the division of tracks into sectors with the sector
markers. Now-a-days low-level formatting is performed by the hard-disk manufactures themselves.
We have data in our hard-disk and when we perform low-level formatting in the presence of data in the
hard-disk all the data have been erased and it is impossible to recover that data. Some users make such
a format that they can avoid their privacy leakage. Otherwise low-level will cause damage to hard-disk
shortens the service-life.
Therefore, this formatting is not suggested to users.
2. Partitioning
As suggesting from the name, partitioning means divisions. Partitioning is the process of dividing the
hard-disk into one or more regions. The regions are called as partitions.
It can be performed by the users and it will affect the disk performance.
3. High-level Formatting
High-level formatting is the process of writing. Writing on a file system, cluster size, partition label,
and so on for a newly created partition or volume. It is done to erase the hard-disk and again installing
the operating system on the disk-drive.
• Implementation is easy.
• No chance of starvation.
Disadvantages:
• 'Seek time' increases.
• Not so efficient.
Explanation: The disk arm searches for the request which will have the least difference in head
movement. So, the least difference is (50-43). Here the difference is not about the shortest value but it
is about the shortest time the head will take to reach the nearest next request. So, after 43, the head will
be nearest to 24, and from here the head will be nearest to request 16, After 16, the nearest request is
82, so the disk arm will move to serve to request 82 and so on.
Hence, Calculation of Seek Time = (50-43) + (43-24) + (24-16) + (82-16) + (140-82) + (170-140) +
(190-170) = 208
Advantages:
• In this algorithm, disk response time is less.
• More efficient than FCFS.
Disadvantages:
• Less speed of algorithm execution.
• Starvation can be seen.
Explanation: In the above image, we can see that the disk arm starts from position 50 and goes in a
single direction until it reaches the end of the disk i.e.- request position 199. After that, it reverses and
starts servicing in the opposite direction until reaches the other end of the disk. This process keeps
going on until the process is executed. Hence, the Calculation of 'Seek Time' will be like: (199-50) +
(199-16) =332
Advantages:
Implementation is easy.
Requests do not have to wait in a queue.
Disadvantage:
The head keeps going on to the end even if there are no requests in that direction.
Explanation: In the above figure, the disk arm starts from position 50 and reached the end(199), and
serves all the requests in the path. Then it reverses the direction and moves to the other end of the disk
i.e.- 0 without serving any task in the path.
After reaching 0, it will again go move towards the largest remaining value which is 43. So, the head
will start from 0 and moves to request 43 serving all the requests coming in the path. And this process
keeps going.
Hence, Seek Time will be
(199−50)+(199−0)+(43−0)=391
Advantages:
The waiting time is uniformly distributed among the requests.
Response time is good in it.
Disadvantages:
The time taken by the disk arm to locate a spot is increased here.
The head keeps going to the end of the disk.
Example a disk having 200 tracks (0-199). The request sequence(82,170,43,140,24,16,190) are shown
in the given figure and the head position is at 50.
Explanation: The disk arm is starting from 50 and starts to serve requests in one direction only but in
spite of going to the end of the disk, it goes to the end of requests i.e.-190. Then comes back to the last
request of other ends of the disk and serves them. And again starts from here and serves till the last
request of the first side. Hence, Seek time =(190-50) + (190-16) =314
Advantages:
Starvation does not occur.
Since the head does not go to the end of the disk, the time is not wasted here.
Disadvantage:
The arm has to be conscious to find the last request.
Stable Storage
Stable storage is a classification of computer data storage technology that guarantees atomicity for any
given write operation and allows software to be written that is robust against some hardware and power
failures. To be considered atomic, upon reading back a just written-to portion of the disk, the storage
subsystem must return either the write data or the data that was on that portion of the disk before the
write operations.
Most computer disk drives are not considered stable storage because they do not guarantee atomic
write; an error could be returned upon subsequent read of the disk where it was just written to in lieu of
either the new or prior data.
Multiple techniques have been developed to achieve the atomic property from weakly atomic devices
such as disks. Writing data to a disk in two places in a specific way is one technique and can be done
by application software.
Most often though, stable storage functionality is achieved by mirroring data on separate disks via
RAID technology (level 1 or greater). The RAID controller implements the disk writing algorithms that
enable separate disks to act as stable storage. The RAID technique is robust against some single disk
failure in an array of disks whereas the software technique of writing to separate areas of the same disk
only protects against some kinds of internal disk media failures such as bad sectors in single disk
arrangements.
Error Handling
Let us analyze the case below taking into consideration that a system user wants to read this file from
his or her local disc drive through an operating system’s functions of input-output. In this case, the
transistor and permanence failure will be discussed during these activities. Additionally, we will treat
this answer as being operating system and how the error-handling is handled by the OS.
Transient Failure
User Action: The user starts the process where a file read operation is being executed to open and
extract report.pdf content. With the aid of this operating systems file I/O API.
Transient Failure: When the user starts this process of reading content from the file, a transient failure
takes place whereby there is an interruption in connection on account of momentary change when
power fluctuates forcing disconnection off disc drive for some time.
Operating System Response (Error Handling): The Operating System for the movement detects that
there was a transient failure while talking to disc drive. Its error handling mechanisms are triggered like
buffer I/O and retries. The operating system will try again or performs retries to resume the
communication with disc drive, and continue file read operation from where it had been interrupted. It
may not be known to the user that this was a temporary malfunction since the operating system
automatically recovers from such transient failure
Permanent Failure
User Action: In this, the user starts for opening a file named data.csv on an external USB device with
help of operating systems’s I /O functions used to access files.
Permanent Failure: As we have seen permanent failure, is essentially resulted from hardware and
software elements. Therefore in a permanent failure, the external USB drive suffers an irreparable
physical wear-and –tear of its read and write head resulting to impossibility or difficulty by that
particular operating system accessing any data from such drive.
Operating System Response (Error Handling): When the Operating System recognizes permanent
failure and tries to interact with a USB device, in this case Direct recovery from hardware deficiency
can not be made by operating systems. In reality, it provides the user an error code that indicates to
what real problem has resulted. Therefore, the error message can show that due to some reason an
external drive has been filled or because of this it cannot be accessed in reality. The user is also
recommended to test its drives connection or seek some consultant help for performing the data
recovery process, as this can resolve his/her problem in favor by making him replace faulty drive with a
new operative one.