Study Material
Semester-05
OS
Unit-05

Unit 5: Input/Output and File Management

I/O Management and Disk Scheduling

Efficient input/output (I/O) management is a critical aspect of operating systems, ensuring that data is read from or written to hardware devices without causing significant system slowdowns. This section covers the key components of I/O management and the algorithms that optimize disk performance.

I/O Devices

I/O devices serve as a medium between the computer and the external environment. They can be broadly classified into two types:

  • Block devices: These devices, such as hard drives and SSDs, read and write data in blocks. They allow random access to data, which means the system can read or write any block at any time.
  • Character devices: Devices like keyboards and mice, which transmit data in streams, one character at a time. These devices provide sequential access, where the data must be accessed in order.

Organization of the I/O Function

The I/O function involves several layers to efficiently manage interactions between hardware devices and system software. The organization can be divided into three primary levels:

  1. I/O controllers: These hardware devices control one or more I/O devices. The controller facilitates communication between the system's CPU and the device itself.
  2. Device drivers: Device-specific software modules that communicate with I/O controllers. Each I/O device has a corresponding driver responsible for translating general commands from the operating system into device-specific instructions.
  3. Operating system I/O management: The OS kernel manages higher-level functions such as I/O scheduling, device independence, and error handling. It provides abstractions to users and applications, allowing easy access to I/O devices.

I/O Buffering

Buffering plays a critical role in handling differences in data transfer speeds between system components. Without buffering, fast devices like the CPU may end up waiting for slower devices, reducing overall system efficiency. There are three main types of I/O buffering:

  • Single buffering: In this method, data is temporarily stored in a buffer while being transferred between the CPU and the device. However, this method can still result in delays due to differences in device speeds.
  • Double buffering: Two buffers are used to ensure that while one buffer is being written to the device, the other can be read or filled by the CPU. This method reduces idle time and improves performance.
  • Circular buffering: Multiple buffers are arranged in a circular manner. As one buffer gets filled and transferred, the CPU can continue using the next available buffer. Circular buffering provides the best performance for high-throughput data streams.

Disk Scheduling Algorithms

Disk scheduling algorithms are used to manage the order in which I/O requests are served. Efficient disk scheduling optimizes the time it takes for the disk arm to move between different tracks and sectors, improving performance and reducing latency. The following are some common disk scheduling algorithms:

First-In-First-Out (FIFO)

The simplest disk scheduling algorithm, FIFO processes requests in the order they arrive. While simple to implement, it may not be the most efficient as it can lead to longer seek times.

Shortest Seek Time First (SSTF)

SSTF selects the request closest to the current position of the disk head. While this reduces seek time, it can lead to starvation, where certain requests are continually deferred because closer requests keep arriving.

SCAN

Also known as the elevator algorithm, SCAN moves the disk head from one end of the disk to the other, serving requests as it moves. Once it reaches the end, it reverses direction and starts servicing requests in the opposite direction. This algorithm is fairer than SSTF, but may still favor requests at the edges of the disk.

C-SCAN

C-SCAN (Circular SCAN) is a variation of SCAN. Instead of reversing direction after reaching the end of the disk, the disk head returns to the beginning of the disk without servicing any requests during the return journey. This ensures that requests are treated more evenly, especially for those located in the middle of the disk.

LOOK

LOOK is a variant of SCAN. Instead of moving the disk head all the way to the edge of the disk, it only moves as far as the furthest request in each direction, reducing unnecessary movement.

C-LOOK

C-LOOK is the circular version of LOOK. The disk head only travels as far as the furthest request in one direction before jumping back to the beginning, ensuring efficient and fair request handling.


File Management

File management is another critical aspect of operating systems, dealing with the storage, retrieval, and organization of files. Efficient file management ensures that users and applications can access data quickly and securely.

Files and File Systems

A file is a collection of related data stored on secondary storage devices. Files can store anything from text to multimedia and can be structured or unstructured. A file system is responsible for organizing and managing these files on the storage medium. It provides an interface for users to interact with files and directories.

File Structure

Files can have different structures based on the type of data they store:

  • Flat files: Store data sequentially without any inherent structure.
  • Structured files: Contain data organized in specific formats, such as databases or spreadsheets.
  • Binary files: Files that store data in binary form, readable only by specific applications.

File Organization and Access

There are several ways to organize and access files, each optimized for different use cases:

  • Sequential access: Files are read or written sequentially, from the beginning to the end. This method is ideal for applications like media players.
  • Direct access: Files can be accessed at random by specifying a block number or an index. This method is used in databases and operating systems where fast retrieval is crucial.
  • Indexed access: A combination of sequential and direct access, indexed access uses an index to locate records within a file. Once located, the record can be accessed sequentially.

File Directories

Files are often grouped into directories (or folders), which provide a way to organize files and manage file hierarchy. Directories offer several operations:

  • Creation and deletion of files: Users can add or remove files from directories.
  • Listing of files: Directories store metadata about the files they contain, such as name, size, and modification date.
  • Path navigation: Directories allow users to specify file locations using paths, either absolute paths (from the root directory) or relative paths (from the current directory).

File Sharing

In multi-user systems, file sharing allows multiple users to access the same files. There are different levels of file sharing permissions:

  • Read: Users can view file contents but cannot modify them.
  • Write: Users can modify file contents.
  • Execute: Users can run executable files.

To manage file sharing, operating systems use access control lists (ACLs) or permissions to define which users or groups have access to a file and what operations they can perform.

Record Blocking

When files are stored on disk, they are often divided into records. These records may not always perfectly fit into disk blocks, which can lead to wasted space or the need for multiple blocks to store a single record. Record blocking is a method of organizing these records into blocks to optimize space usage and access time. There are three main types of record blocking:

  1. Fixed blocking: Fixed-size records are grouped into fixed-size blocks.
  2. Variable blocking: Variable-sized records are grouped into fixed-size blocks.
  3. Spanning: Records that do not fit within a single block are split across multiple blocks.

Secondary Storage Management

Managing secondary storage efficiently is crucial for maintaining system performance and data integrity. Secondary storage includes devices like hard drives, SSDs, and optical disks. The operating system handles several tasks related to secondary storage management:

  • Free space management: The OS keeps track of which parts of the disk are free and which are in use. Common methods include bitmaps and linked lists.
  • Allocation of space: Files are allocated space on the disk using different methods such as contiguous allocation, linked allocation, or indexed allocation.
  • Disk fragmentation: Over time, files can become fragmented, meaning their data is scattered across different parts of the disk. Defragmentation tools are used to reorganize files and reduce fragmentation, improving access time.