# File Concepts

To a user, the file is the smallest unit of storage on a computer system. The user performs file operations such as open, close, read, write and modify. They can create or delete files from the system. In this article, you will learn the basics of file and file system.

Contents

### File Concept

Computer store information in storage media such as disk, tape drives, and optical disks. The operating system provides a logical view of the information stored in the disk. This logical storage unit is a file.

The information stored in files are non-volatile, means they are not lost during power failures. A file is named collection of related information that is stored on physical storage.

Data cannot be written to a computer unless it is written to a file. A file, in general, is a sequence of bits, bytes, lines, or records defined by its owner or creator. The file has a structure defined by its owner or creator and depends on the file type.

• Text file – It has a sequence of characters.
• Image file – It has visual information such as photographs, vectors art and so on.
• Source file – It has subroutines and function that are compiled later.
• Object file – It has a sequence of bytes, organized into bytes and used by the linker.
• Executable file – The binary code that the loader brings to memory for execution is stored in an exe file.

### File Attributes

A file has a single e editable name given by its creator. The name never changes unless a user has necessary permission to change the file name. The names are given because it is humanly understandable.

The properties of a file can be different on many systems, however, some of them are common:

1. Name – unique name for a human to recognize the file.
2. Identifier – A unique number tag that identifies the file in the file system, and non-human readable.
3. Type – Information about the file to get support from the system.
4. Size – The current size of the file in bytes, kilobytes, or words.
5. Access Control – Defines who could read, write, execute, change, or delete the file in the system.
6. Time, Date, and User identification – This information kept for date created, last modified, or accessed and so on.

The information about the file is kept in a directory structure which also resides on the secondary storage.

### File Operations

The operating system performs the following file operations using various system calls:

1. Create
3. Write
4. Delete
5. Reposition
6. Truncate files

Create Files – User must have necessary disk space on the file system to create a file. A directory entry is required where the file is created.

Read Files – The system call requires file name and next block in memory to be read. The system needs a read pointer to read the file from a specific location in the file and this pointer is updated for the next read from the file.

Write Files – The system call uses same file pointers of the process to write to a file. This saves space and reduces complexity.

Repositioning within a file – The current-file-position pointer is repositioned to a given value. It does not involve an I/O, and known as file seek operation.

Deleting a file – We look into the directory for the file name, if a file is found, release the space occupied by the file and remove directory entries for the deleted file.

Truncating a file – Sometimes the user does not want to delete a file, but remove some information from it. This will change file length attribute, however, other attributes remain unchanged.

There are other file operations such asappending a file,renaming a file, create a duplicate copy of the file.

### Open-File Table

The file operations require searching a directory every time. To avoid frequent searches, the OS allows a system call – open() and keeps an.open file table When the file operation is requested, the system refers to the file via an index value. The file is not used actively, the process closes the file and the OS remove its entry from the open file table.

The open() system call can take mode information such as read-only, write, append, create, and so on. The mode is checked against the file permission and it allowed file is open for the process.

The open call returns a pointer to the entry in the open-file table and the pointer, not the file is used for all I/O operations.

Several different files may open the same file, therefore, the system maintains two file-tables – per-process table and system-wide table. The per-process table contains information about files opened by the file.
For example, current file-pointer for each file, access rights, and accounting information for the files.

Each entry in the process file-table points to a system-wide table, which contains process independent information such as the location of the file on the disk, access dates, and file size. When a process opens a file, an entry is added to the processor open file table of the process and points to the entry in the system-wide table.

The open file table keeps an open count indicating the number of processes that have opened the file. The close operation decreases the count and when it reaches 0 the file entry is removed from the open-file table.

The following information is associated with an open file:

File pointer – It helps in read() and write() operation. It is unique to each process.
File Open count – When multiple processes open the same file, therefore, file open-count keep track of open files and closed files and when the count reaches 0 and process can remove the entry from the open-table.
Disk location of the file – To modify data the path to the file is kept in memory. The system does not have to read it for each operation.
Access rights – The process opens the file in access mode and information regarding the access rights is in the per-process table, based on which OS can reject or accept the I/O request.

Apart from the above, some OS provides ways to lock files. This is true in the case of shared files where only one process can write to the file while other processes read the information on the file.

### References

• Abraham Silberschatz, Peter B. Galvin, Greg Gagne (July 29, 2008) Operating System Concepts, 8 edition edn., Wiley.