1. Introduction

When a program works with files, it does not directly access the disk. Instead, it uses system calls provided by the operating system.

Examples:

open("file.txt");

read(fd, buffer, size);

write(fd, buffer, size);

Although these operations appear simple, they involve complex interactions between:

  • User Space

  • Operating System Kernel

  • Storage Hardware

File operations form the interface between user programs and persistent storage.

2. What are File Operations?

File operations are system-level functions that allow programs to create, access, modify, and manage files.

Key Idea

User Program
      ↓
 System Call
      ↓
 Operating System
      ↓
 File System
      ↓
 Disk / SSD

3. Major File Operations

The fundamental file operations are:

  • Create

  • Open

  • Read

  • Write

  • Append

  • Seek

  • Close

  • Delete

Each operation triggers specific actions inside the operating system.

4. Create Operation

Purpose

Creates a new file in the file system.

Example

creat("file.txt");

Internal Steps

  1. OS checks whether the file already exists.

  2. Allocates a new inode (or FCB).

  3. Creates a directory entry.

  4. Initializes metadata:

    • Size = 0

    • Owner

    • Permissions

    • Timestamps

  5. Optionally allocates storage space.

Key Insight

Creating a file mainly means creating its metadata structures, not storing data.

5. Open Operation (Very Important)

Purpose

Before a file can be read or written, it must be opened.

Example

fd = open("file.txt", mode);

Internal Steps

  1. Search file using directory structure.

  2. Load inode/FCB into memory.

  3. Verify access permissions.

  4. Create an entry in:

    • System-wide Open File Table

    • Per-Process File Table

  5. Initialize file pointer.

  6. Return a file descriptor.

File Descriptor

A file descriptor (fd) is a small integer that acts as a handle to the opened file.

Key Insight

Programs never manipulate files directly—they use file descriptors returned by the OS.

6. File Open Flow

Application
      ↓
open("file.txt")
      ↓
Directory Lookup
      ↓
inode / FCB Loaded
      ↓
Permission Check
      ↓
Open File Table Entry Created
      ↓
File Descriptor Returned

7. Read Operation

Purpose

Retrieves data from a file.

Example

read(fd, buffer, size);

Internal Steps

  1. Use file descriptor to locate file.

  2. Access open file table entry.

  3. Read current file pointer.

  4. Convert logical offset into disk block location.

  5. Load required block into buffer cache.

  6. Copy data into user buffer.

  7. Advance file pointer.

Key Insight

Reading a file involves multiple layers of memory movement before data reaches the program.

8. Write Operation

Purpose

Stores data into a file.

Example

write(fd, buffer, size);

Internal Steps

  1. Locate file through descriptor.

  2. Determine target disk blocks.

  3. Copy data from user buffer to kernel buffer.

  4. Mark affected blocks as dirty.

  5. Schedule disk write.

  6. Update metadata and file pointer.

Important Concept: Buffered Writing

Most operating systems do not write data immediately to disk.

Instead:

User Buffer
      ↓
Kernel Buffer Cache
      ↓
Disk (later)

Key Insight

Write operations are often delayed for better performance.

9. Append Operation

Purpose

Adds data at the end of a file.

Example

open("file.txt", O_APPEND);

Internal Steps

  1. Move file pointer to file end.

  2. Write new data.

  3. Update file size.

Benefit

Existing content remains unchanged.

10. Seek Operation

Purpose

Moves the file pointer to a specific position.

Example

lseek(fd, offset, SEEK_SET);

Uses

  • Random file access

  • Skipping data

  • Updating specific locations

Example

Current Position = 100

lseek(fd, 500, SEEK_SET)

New Position = 500

Key Insight

Seek changes the file pointer without reading or writing data.

11. Close Operation

Purpose

Releases resources associated with an opened file.

Example

close(fd);

Internal Steps

  1. Remove process file table entry.

  2. Decrease reference count.

  3. Flush pending writes.

  4. Release kernel resources.

Key Insight

A file is not completely finished until it is properly closed.

12. Delete Operation

Purpose

Removes a file from the file system.

Example

unlink("file.txt");

Internal Steps

  1. Remove filename from directory.

  2. Decrease link count.

  3. If link count becomes zero:

    • Free disk blocks

    • Remove inode/FCB

  4. Reclaim storage space.

Key Insight

Deleting a file often removes its directory entry first. Actual storage may be released later.

13. File Pointer (Critical Concept)

Every opened file maintains a file pointer.

Purpose

Tracks the current position within the file.

Example

File Content:

ABCDEFGHIJ

After reading 3 bytes:

Read: ABC

File Pointer → Position 3

Next read starts from:

DEF...

Benefits

  • Sequential access

  • State preservation

  • Efficient file processing

14. Operating System Data Structures

14.1 Per-Process File Table

Maintained separately for each process.

Stores:

  • File descriptor

  • Access mode

  • Pointer to system file table

14.2 System Open File Table

Shared by the entire system.

Stores:

  • File location

  • Current status

  • Reference count

  • File pointer

14.3 inode / FCB

Stores file metadata:

  • Size

  • Owner

  • Permissions

  • Disk block locations

  • Timestamps

Relationship

Process
   ↓
File Descriptor
   ↓
Per-Process File Table
   ↓
System Open File Table
   ↓
inode / FCB
   ↓
Disk Blocks

15. Sharing Open Files

Multiple processes can open the same file simultaneously.

The operating system manages this through:

  • Reference counts

  • Shared open file entries

  • Synchronization mechanisms

Key Insight

Many processes may access one file, but the OS ensures consistency.

16. Performance Optimizations

Buffer Cache

Frequently accessed blocks are cached in RAM.

Disk → Cache → Process

Reduces disk access time.

Read-Ahead

OS predicts future reads and loads data in advance.

Write-Behind (Write-Back)

OS delays writes and batches them together.

Benefits

  • Faster execution

  • Reduced disk I/O

  • Better throughput

17. Real-World Analogy

Think of a library.

File OperationLibrary Equivalent
CreateAdd a new book
OpenBorrow a book
ReadRead pages
WriteAdd notes to book
SeekJump to a page
CloseReturn the book
DeleteRemove the book from library

File operations appear simple to programmers, but internally they involve directory lookup, metadata management, caching, buffering, access control, and communication between user space, kernel space, and storage devices.