File Operations - aryanjoshi0823/5143-Operating-System GitHub Wiki

File operations are essential functionalities provided by operating systems (OS) to enable users and applications to create, manipulate, and manage files on a storage device. These operations form the backbone of data interaction and are implemented to balance performance, reliability, and security.


File Access Lifecycle

  1. Opening a File

    • Files must be explicitly opened before access. This sets up the necessary context, such as associating a file descriptor with the file.
    • In rare cases, some systems use automatic file handling, opening files at first access and managing closure automatically.
  2. Closing a File

    • Files must be explicitly closed to release system resources and ensure that buffered data is written to disk.
    • Failure to close files can result in resource leaks and potential data corruption.

Core File Operations

  1. Creating a File

    • Allocates space on the storage device and creates an entry in the directory structure.
    • Example: open() with O_CREAT in Linux.
  2. Opening a File

    • Prepares the file for read, write, or append operations by associating a file descriptor or handle.
    • Modes: Read-only, Write-only, Read/Write, Append.
  3. Reading from a File

    • Retrieves data from the file starting at the current file pointer position.
    • Example: read(fd, buffer, count) reads count bytes into buffer.
  4. Writing to a File

    • Stores data into the file at the current file pointer position.
    • Example: write(fd, buffer, count) writes count bytes from buffer.
  5. Seeking within a File

    • Moves the file pointer to a specific position for random access.
    • Example: lseek(fd, offset, whence) adjusts the pointer based on whence (start, current, end).
  6. Deleting a File

    • Removes the file entry and marks the space as free.
    • Example: unlink(filename) in Unix/Linux.
  7. Renaming a File

    • Changes the name or location of a file.
    • Example: rename(oldname, newname).
  8. Closing a File

    • Releases the file descriptor or handle.
    • Example: close(fd).

Open File Table

The OS maintains an open file table to manage all files currently in use. Key elements of the table include:

  1. File Pointer

    • Tracks the current position for the next read or write operation.
  2. File-Open Count

    • Indicates how many processes have the file open.
    • When the count reaches zero, the file is removed from the table.
  3. Disk Location

    • Specifies the physical storage location of the file.
  4. Access Rights

    • Defines the permissions for interacting with the file.

File Locking

File locking ensures safe and coordinated access in multi-process environments. Types of locks:

  1. Shared Lock

    • Allows multiple processes to read a file simultaneously.
  2. Exclusive Lock

    • Restricts file access to a single process for both reading and writing.
  3. Advisory Lock

    • A non-enforced signal that other processes may choose to ignore (like a "Keep Out" sign).
  4. Mandatory Lock

    • Enforces strict access restrictions, acting like a locked door.

Buffering and Caching

  • Buffers: Temporarily store data in memory to reduce I/O overhead.
  • Caching: Consolidates multiple small operations into fewer physical disk accesses for efficiency.