- A file is an abstract data type. To define a file properly, we need to consider the operations that can be performed on files.
- Six basic file operations. The OS can provide system calls to create, write, read, reposition, delete, and truncate files.
- Creating a file. Two steps are necessary to create a file.
- Space in the file system must be found for the file.
- An entry for the new file must be made in the directory.
- Writing a file. To write a file, we make a system call specifying both the name of the file and the information to be written to the file. The system must keep a write pointer to the location in the file where the next write is to take place. The write pointer must be updated whenever a write occurs.
- Reading a file. To read from a file, we use a system call that specifies the name of the file and where (in memory) the next block of the file should be put. The system needs to keep a read pointer to the location in the file where the next read is to take place.
- Because a process is usually either reading from or writing to a file, the current operation location can be kept as a per-process current-file-position pointer.
- Both the read and write operations use this same pointer, saving space and reducing system complexity.
- Repositioning within a file. The directory is searched for the appropriate entry, and the current-file-position pointer is repositioned to a given value. Repositioning within a file need not involve any actual I/O. This file operation is also known as a file seek.
- Deleting a file. To delete a file, we search the directory for the named file. Having found the associated directory entry, we release all file space, so that it can be reused by other files, and erase the directory entry.
- Truncating a file. The user may want to erase the contents of a file but keep its attributes. Rather than forcing the user to delete the file and then recreate it, this function allows all attributes to remain unchanged (except for file length) but lets the file be reset to length zero and its file space released.
Figure 10.2:
File operations.
|
- These six basic operations comprise the minimal set of required file operations.
- These primitive operations can then be combined to perform other file operations (i.e., copying).
- The OS keeps a small table, called the open-file table, containing information about all open files.
- When a file operation is requested, the file is specified via an index into this table, so no searching is required.
- When the file is no longer being actively used, it is closed by the process, and the OS removes its entry from the open-file table.
- Most systems require that the programmer open a file explicitly with the system call before that file can be used.
- The operation takes a file name and searches the directory, copying the directory entry into the open-file table.
- This call can also accept access-mode information (create, read-only, read-write, append-only, and so on). This mode is checked against the file's permissions. If the request mode is allowed, the file is opened for the process.
- The system call typically returns a pointer to the entry in the open-file table. This pointer, not the actual file name, is used in all I/O operations.
- The implementation of the and operations is more complicated in an environment where several processes may open the file at the same time. This may occur in a system where several different applications open the same file at the same time.
- Typically, the OS uses two levels of internal tables:
- A per-process table. The per-process table tracks all files that a process has open. For instance, the current file pointer for each file is found here. Access rights to the file and accounting information can also be included.
- A system-wide table. Each entry in the per-process table in turn points to a system-wide open-file table. The system-wide table contains process-independent information, such as the location of the file on disk, access dates, and file size. Once a file has been opened by one process, the system-wide table includes an entry for the file.
- Typically, the open-file table also has an open count associated with each file to indicate how many processes have e the file open.
- Each decreases this open count, and when the open count reaches zero, the file is no longer in use, and the file's entry is removed from the open-file table.
- In summary, several pieces of information are associated with an open file.
- File pointer.
- File-open count.
- Disk location of the file. The information needed to locate the file on disk is kept in memory so that the system does not have to read it from disk for each operation.
- Access rights. Each process opens a file in an access mode. This information is stored on the per-process table so the OS can allow or deny subsequent I/O requests.
- Some OSs provide facilities for locking an open file (or sections of a file). File locks allow one process to lock a file and prevent other processes from gaining access to it. File locks are useful for files that are shared by several processes -for example, a system log file that can be accessed and modified by a number of processes in the system.
Cem Ozdogan
2011-02-14