FILE OPERATIONS



FILE ATTRIBUTES:
  • A file's attributes vary from one OS to another but typically consist of these:
    • Name.
    • Identifier. This unique tag, usually a number, identifies the file within the file system; it is the non-human-readable name for the file.
    • Type.
    • Location. This information is a pointer to a device and to the location of the file on that device.
    • Size. The current size of the file (in bytes, words, or blocks) and possibly the maximum allowed size are included in this attribute.
    • Protection. Access-control information determines who can do reading, writing, executing, and so on.
    • Time, date, and user identification. This information may be kept for creation, last modification, and last use.
FILE OPERATIONS:
  • 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.
      1. Space in the file system must be found for the file.
      2. 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.
  • 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.
  • 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.
  • Several pieces of information that 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.

No comments:

Post a Comment