File System in Operating System (OS)

Learn via video courses
Topics Covered

What is a Filesystem in OS?

A file system in OS dictates how the contents of a storage medium are stored and organized. These storage media (such as secondary memory, external drives, etc) could be computer secondary memory, flash memory, etc. The contents are either files or directories. Most of the time, a storage device has a number of partitions. Each of these partitions is formatted with an empty filesystem for that device. A filesystem helps in separating the data on the storage into comparatively smaller and simpler segments. These chunks are files and directories. The filesystem also provides for storing data related to files, such as their name, extension, permissions, etc.

file system in OS

Advantages of using a file system in OS

  1. Organized Data Storage: A file system provides a structured way to store and organize data on storage devices, making it easy to locate and access files.

  2. Efficient Data Retrieval: It enables quick and efficient retrieval of files by providing a hierarchical directory structure and indexing mechanisms.

  3. Data Security and Permissions: File systems offer security features like access control lists and file permissions, ensuring that only authorized users can access or modify specific files.

  4. Data Integrity and Reliability: They include features like journaling and checksums to maintain data integrity and protect against data corruption or loss due to unexpected events.

  5. Space Management: File systems handle allocation and deallocation of storage space, preventing issues like fragmentation and ensuring optimal use of available space.

Disadvantages of using a file system in OS

  1. Limited File Naming Conventions: Some file systems impose restrictions on file names, such as maximum length or prohibited characters, which can be limiting for users.

  2. Potential for Fragmentation: Over time, file systems can become fragmented, meaning that files are stored in non-contiguous blocks on the storage device, potentially leading to reduced performance.

  3. Security Vulnerabilities: While file systems provide security features, they can still be susceptible to vulnerabilities and attacks if not properly configured or updated.

  4. Overhead for Small Files: File systems may allocate a minimum block size for each file, which can result in wasted space for small files, leading to less efficient use of storage.

  5. Complex Maintenance: Managing a file system, especially on large storage systems, can be complex and may require regular maintenance tasks such as defragmentation, disk cleanup, and periodic backups.

File Structure in OS

A file is a logical unit of information. They are produced by processes. The operating system manages files. While creating a file, a name is assigned to it. After this process has terminated, the file exists and remains accessible to other processes. The name of a file has two parts, separated by a dot. Example, code.cpp is a c++ program file with name code and extension cpp. The name of the file before the dot is a label for the file's identification, while the part after the dot is called the file extension which indicates the type of the file.

File Attributes in OS

File attributes are configuration and information related to files. These attributes grant/deny requests of a user/process for access, modifying, relocating, or deleting it. Some common examples of file attributes are:

  • Read-only: Allows a file to be only read.
  • Read-Write: Allows a file to be read and written to.
  • Hidden: File is made invisible during non-privileged regular operations.
  • Execute: Allows a file to be executed like a program. Other than these there are several attributes. Many of them are platform-specific.

File Access Mechanisms in OS

Following are the 3 types of file accessing mechanisms in an operating system:

1. Direct

This method represents a file's disk model. Just like a disk, direct access mechanism allows random access to any file block. A file is divided into a number of blocks. These blocks are of the same size. The file is viewed as an ordered sequence of these blocks. Thus the OS can do a read-write operation upon any random block. Following are the three operations under direct mechanism:

  1. Read x: read contents of block x
  2. Write x: write to block x
  3. Goto x: jump to block x

2. Sequential Access

This is a simple way to access the information in a file. Contents of a file are accessed sequentially (one record after another). This method is used by editors and compilers. Tape drives use a sequential method, processing a memory block at a time. They were a form of the storage medium in computers used in earlier times. Following are the 3 operations in sequential access:

  1. Read next: read the next portion of the file.
  2. Write next: add a node to the end of the file and move the pointer to it.
  3. Reset: move the pointer to the starting of the file.

3. Indexed Access Method

This method is a variant of the direct access method. It maintains an index that contains the addresses of the file blocks. To access a record, the OS will first search its address in the index which will then point to the actual address of that block of the file that has the required record.

Properties of a File System

  • Files are stored on a storage medium such as disk and do not vanish when a user logs out of the computer system.

  • With each file are associated access permissions, which permit controlled sharing of that file.

  • Files may form arranged or complex structures according to the relationship among them.

  • Several files can be grouped together under a directory.

  • A directory also referred to as a folder also has attributes similar to those of a file, such as a name, size, location, access permissions, etc.

  • A file system also provides several features such as a crash recovery mechanism, data loss/corruption prevention, etc.

File Types

There are a large number of file types. Each has a particular purpose. The type of a file indicates its use cases, contents, etc. Some common types are:

1. Media:

Media files store media data such as images, audio, icons, video, etc. Common extensions: img, mp3, mp4, jpg, png, flac, etc.

2. Programs:

These files store code, markup, commands, scripts, and are usually executable. Common extensions: c, cpp, java, xml, html, css, js, ts, py, sql, etc.

4. Operating System Level:

These files are present with the OS for its internal use. Common extensions: bin, sh, bat, dl, etc.

5. Document:

These files are used for managing office programs such as documents, spreadsheets, etc. Common extensions: xl, doc, docx, pdf, ppt, etc.

6. Miscellaneous:

Generic text file(.txt), canvas files, proprietary files, etc.

File Types in an OS

There are numerous file types that an operating system uses internally and are not generally used or required by the system user. These files could be application software files, kernel files, configuration files, metadata files, etc. Windows supports the following two file types:

1. Regular Files

Regular files consist of information related to the user. The files are usually either ASCII or binary. ASCII files contain lines of text. The major benefit of an ASCII file is that it can be displayed or printed as it is, and it can be edited using a text editor.

Binary files on printing may give some random junk content. Usually, a binary file would have some sort of internal structure that is only known to the program that uses it. A binary file is a sequence of bytes, which if is in the proper format, can be executed by the operating system. Regular files are supported by both Windows as well as UNIX-based operating systems.

2. Directories

A directory in the filesystem is a structure that contains references to other files and possibly other directories. Files could be arranged by storing related files in the same directory. Directories are supported by both Windows as well as UNIX-based operating systems.

3. Character Special Files

A character special file provides access to an I/O device. Examples of character special files include a terminal file, a system console file, a NULL file, a file descriptor file, etc.

Each character special file has a device major number and a device minor number. The device major number associated with a character special file identifies the device type. The device minor number associated with a character special file identifies a specific device of a given device type. Character special files are supported by UNIX-based operating systems.

4. Block Special Files

Block special files enable buffered access to hardware devices They also provide some abstraction from their specifics. Unlike character special files, block special files always allow the programmer to read and write a block of any size or alignment. Block special files are supported by UNIX-based operating systems.

Functions of File in OS

  • They are used for storing data in a computer.
  • They enable the separation of data according to some criteria.
  • They enable efficient, simple, organized access to data.
  • They help in isolating sensitive or important data from the rest of the data.
  • They enable locating particular data items in the storage medium.

Commonly Used Terms in File systems

1. File

A file is a logical unit of information created by processes that processes produce.

2. Directory

A location on the storage that stores several files within itself.

3. Partition

A part of the storage medium is virtually separate from the rest of the storage.

4. Access Mechanism

The process is followed by the OS to grant a user/process access to a file.

5. File Extension

A label appended to the name of a file after a dot. Gives information of the purpose of and information in the file.

File Directories

A directory is a location on the disk that stores files within itself in a computer system. Directories are used in hierarchical file systems. folder and directory are interchangeable terms.

In the hierarchy, there is a directory at the top referred to as the root directory. It's so-called because there is nothing beyond it. The other directories are branches to it.

In many operating systems, files and directories can be hidden by prepending a period to their name (.name). Directories can be nested in each other.

File Allocation Methods

Following are the three methods of space allocation in an operating system:

1. Contiguous

In this method, every file occupies a set of consecutive addresses on the storage (secondary memory/disk). Each entry in a directory contains a number of blocks, starting address of the first block, and the file name. During writing, if the file size is expandable, then either extra space is left, or the file is copied somewhere else leaving no extra space behind.

file system in OS

2. Indexed

A set of pointers is maintained in an index table. The index table is in turn stored in several index blocks. In an index block, the ith entry (the pointer at position i) holds the disk address of the ith file block.

file system in OS

3. Linked

Each data block in the file contains the address of the next block. Each entry in a directory contains file-name, block address, and (not necessarily) a pointer to the last block. In this file allocation method, each file is treated as a linked list of disks blocks. In the linked space allocation method, it is not necessary that disk blocks be assigned to a file in a contiguous (consecutive) manner on the disk.

file system in OS

Conclusion

  • A file system controls the way the contents of a storage medium are stored, organized, and accessed.

  • A file is a logical unit of information created by processes and managed by the OS.

  • File attributes are configuration and information associated with files.

  • There are three types of file accessing mechanisms in operating systems, namely, indexed, direct and sequential.

  • There are a large number of file types, such as media, programs, files internal to OS, documents, etc.

  • There are three methods of space allocation in a filesystem, namely, contiguous, indexed, and linked.

  • In contiguous space allocation, In this method, every file occupies a set of consecutive addresses on the storage.

  • Indexed space allocation method maintains a set of pointers is in an index table.

  • In Linked space allocation each data block in the file contains the address of the next block. Each entry in a directory contains file-name, block address, and a pointer to the last block.