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
OS checks whether the file already exists.
Allocates a new inode (or FCB).
Creates a directory entry.
Initializes metadata:
Size = 0
Owner
Permissions
Timestamps
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
Search file using directory structure.
Load inode/FCB into memory.
Verify access permissions.
Create an entry in:
System-wide Open File Table
Per-Process File Table
Initialize file pointer.
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
Use file descriptor to locate file.
Access open file table entry.
Read current file pointer.
Convert logical offset into disk block location.
Load required block into buffer cache.
Copy data into user buffer.
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
Locate file through descriptor.
Determine target disk blocks.
Copy data from user buffer to kernel buffer.
Mark affected blocks as dirty.
Schedule disk write.
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
Move file pointer to file end.
Write new data.
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
Remove process file table entry.
Decrease reference count.
Flush pending writes.
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
Remove filename from directory.
Decrease link count.
If link count becomes zero:
Free disk blocks
Remove inode/FCB
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 Operation | Library Equivalent |
|---|---|
| Create | Add a new book |
| Open | Borrow a book |
| Read | Read pages |
| Write | Add notes to book |
| Seek | Jump to a page |
| Close | Return the book |
| Delete | Remove 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.