File Access Methods in OS

Learn via video courses
Topics Covered

Overview

System files and data are kept in the computer system's memory, and when these files are needed by an application, the operating system must have some way to read the memory and access the appropriate files. There are three file access methods in OS, sequential access, direct access, and indexed sequential access.

What are File Access Methods in OS?

A file is a collection of bits/bytes or lines that is stored on secondary storage devices like a hard drive (magnetic disks).

File access methods in OS are nothing but techniques to read data from the system's memory. There are various ways in which we can access the files from the memory like:

  • Sequential Access
  • Direct/Relative Access, and
  • Indexed Sequential Access.

These methods by which the records in a file can be accessed are referred to as the file access mechanism. Each file access mechanism has its own set of benefits and drawbacks, which are discussed further in this article.

Types of File Access Methods in the Operating System

1. Sequential Access

The operating system reads the file word by word in a sequential access method of file accessing. A pointer is made, which first links to the file's base address. If the user wishes to read the first word of the file, the pointer gives it to them and raises its value to the next word. This procedure continues till the file is finished. It is the most basic way of file access. The data in the file is evaluated in the order that it appears in the file and that is why it is easy and simple to access a file's data using a sequential access mechanism. For example, editors and compilers frequently use this method to check the validity of the code.

sequential-access-mechanism

Advantages of Sequential Access:

  • The sequential access mechanism is very easy to implement.
  • It uses lexicographic order to enable quick access to the next entry.

Disadvantages of Sequential Access:

  • Sequential access will become slow if the next file record to be retrieved is not present next to the currently pointed record.
  • Adding a new record may need relocating a significant number of records of the file.

2. Direct (or Relative) Access

A Direct/Relative file access mechanism is mostly required with the database systems. In the majority of the circumstances, we require filtered/specific data from the database, and in such circumstances, sequential access might be highly inefficient. Assume that each block of storage holds four records and that the record we want to access is stored in the tenth block. In such a situation, sequential access will not be used since it will have to traverse all of the blocks to get to the required record, while direct access will allow us to access the required record instantly.

The direct access mechanism requires the OS to perform some additional tasks but eventually leads to much faster retrieval of records as compared to sequential access.

direct-access-mechanism

Advantages of Direct/Relative Access:

  • The files can be retrieved right away with a direct access mechanism, reducing the average access time of a file.
  • There is no need to traverse all of the blocks that come before the required block to access the record.

Disadvantages of Direct/Relative Access:

  • The direct access mechanism is typically difficult to implement due to its complexity.
  • Organizations can face security issues as a result of direct access as the users may access/modify the sensitive information. As a result, additional security processes must be put in place.

3. Indexed Sequential Access

It's the other approach to accessing a file that's constructed on top of the sequential access mechanism. This method is practically similar to the pointer-to-pointer concept in which we store the address of a pointer variable containing the address of some other variable/record in another pointer variable. The indexes, similar to a book's index (pointers), contain a link to various blocks present in the memory. To locate a record in the file, we first search the indexes and then use the pointer-to-pointer concept to navigate to the required file.

Primary index blocks contain the links of the secondary inner blocks which contain links to the data in the memory.

indexed sequential access mechanism

Advantages of Indexed Sequential Access:

  • If the index table is appropriately arranged, it accesses the records very quickly.
  • Records can be added at any position in the file quickly.

Disadvantages of Indexed Sequential Access:

  • When compared to other file access methods, it is costly and less efficient.
  • It needs additional storage space.

Conclusion

  • A file is a collection of bits/bytes or lines that is stored on secondary storage devices like a hard drive.
  • There are three file access methods in OS:
    • Sequential Access,
    • Direct Access, and
    • Indexed Sequential Access.
  • In sequential access, the operating system reads the file word by word in a sequential manner to enable quick access to the next entry.
  • In direct access, there is no need to traverse all of the blocks that come before the required block to access the record.
  • In indexed sequential access, we maintain an index table that appropriately arranges the pointers to access the desired records very quickly.