How to manage open files in operating systems

Processes are often dependent on file structures where code needs to be read from or written. For instance, an executing process might require reading text from a .txt file for per-processingThis includes the removal of all punctuation marks from the text to enable the training of models. purposes, and then writing the refined text into a new file. This alone requires two different open instructions.

1 of 12

It is essential to understand that files are stored on the disk in block structures. From there, they need to be moved to the main memory to be used and accessible for any executing process.

Note: Reading files to and from the disk at every instant some information is required is extremely time-consuming. To eradicate this latency, files with a high probability of being accessed repeatedly are maintained in an open-file tableThis is a mapping that keeps track of currently open files that can be accessed directly from the main memory. that indicates their immediate availability.

Aligning open files

The open(filename) command that instructs the need for a specific file is administered at the running program's end.

To manage and efficiently keep track of open files, several different information structures are required:

  • File pointer: For every process that requires access to a file, there is a distinct pointer that points to the location within the file that was the last read from or written to.

  • Open-file table: This is a table that contains entries of the file control block of every open file and the count of programs accessing the respective file.

  • File-open count: This is a counter store of each file entry against the corresponding count of programs accessing it.

  • Access rights: This addresses the level of access that is provided to each program for the respective file opened. For instance, one program might be only allowed to write to a file, whereas another program might be only allowed to read from the file.

  • Disk location: This refers to the actual disk location of the file.

Types of file tables

There are different kinds of file tables that are used by the operating system:

  • Per-process open file table: This is the table exclusive to each process that stores the information for files currently open by the respective process.

  • System-wide open file table: This is the global file table that contains collective information and mappings for all the files that are open by each of the processes running.

File locking

This is an added optimization that is provided by some operating systems. It primarily enables the consistency of data Verification that the data read from the same file by two different processes is the same, and changes made to the file by one process are in time reflected for the other processwhen a single file is accessed by several processes at concurrent levels of execution.

Shared locks

Shared locks allow several processes to read simultaneously from the same file. This approach does not pose any risks of inconsistencies because both processes only read to and from the file. Hence, data read by one process is the same as the data read by another process.

Exclusive locks

Exclusive locks do not allow processes to be able to read or write to a file, unless no other process writes to the respective file of interest.

Note: This implies that a file that is being written onto by a process would gather an exclusive lock over the file. This would prevent any other file from writing to it or reading from it to prevent inconsistencies.

Conclusion

On the whole, managing file requisites for different processes is essential for cutting down the time required to make disk accesses, and making otherwise hefty overheads topple to an optimal level.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved