Course contentsShow
← Computer Science
Lesson 1387 of 2,87232. File SystemsFree lesson

File Descriptors and File Tables

Understand how the OS tracks open files using file descriptors and per-process file descriptor tables.

File Descriptors and File Tables

You'll learn: How the operating system tracks which files your process has open using file descriptors and internal tables.

What Are File Descriptors?

When a process opens a file, the OS doesn't hand back the entire file or even its path—it returns a small non-negative integer called a file descriptor. Think of it like a ticket stub at a coat check: you don't carry your coat around, just a number that lets you retrieve it later.

Common file descriptors you've likely seen:

  • 0 = standard input (stdin)
  • 1 = standard output (stdout)
  • 2 = standard error (stderr)

Any files you open get descriptors starting from 3 and counting up.

How the OS Tracks Open Files

Behind the scenes, the OS maintains two key data structures:

Per-Process File Descriptor Table

Each process has its own table that maps file descriptor numbers to entries in the system-wide file table. This is your coat check counter—unique to your process.

System-Wide Open File Table

This global table tracks metadata about each open file: current position (offset), access mode (read/write), reference count, and a pointer to the file's inode. Multiple processes can reference the same entry if they share an open file.

Inode Table

Each entry here represents the actual file on disk—its location, size, permissions. The open file table points here.

When you use a file descriptor in a system call (like read or write), the OS quickly follows these pointers: descriptor → open file entry → inode → disk blocks.

Key Takeaway: File descriptors are small integers that index into per-process tables, which point to shared system-wide file entries, enabling efficient tracking and sharing of open files across processes.