A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

File Descriptor Tables and I/O Redirection

College Depth 94 in the knowledge graph ☐ I know this ☆ Set as goal
30topics build on this
372prerequisites beneath it
See this on the map →
File System ConceptsProcesses and the Process Control Block→→Device Drivers and I/O ControllersMemory-Mapped Files and I/O
file-descriptors io redirection

Core Idea

Each process maintains a file descriptor table mapping small integers to open files, sockets, and pipes. Standard file descriptors 0 (stdin), 1 (stdout), and 2 (stderr) are inherited from the parent and can be redirected by closing and reopening different files. File descriptor manipulation is the basis for shell I/O redirection, piping, and inter-process communication.

Explainer

From your understanding of file systems and processes, you know that the OS manages open files on behalf of processes and that each process runs in its own isolated environment. The file descriptor table is the mechanism that connects these two ideas: it is a per-process array where each entry maps a small non-negative integer (the file descriptor) to an entry in the kernel's system-wide open file table. When a process calls `open()`, the kernel finds the lowest available integer in the process's table, fills it in with a pointer to the file's kernel state, and returns that integer. All subsequent operations — `read()`, `write()`, `close()` — use this integer as a handle.

The convention that file descriptors 0, 1, and 2 correspond to standard input, standard output, and standard error is just that — a convention. Nothing in the kernel enforces these meanings. What makes the convention work is that the shell sets up these three descriptors before launching any program, and programs are written to read from 0 and write to 1 and 2 by default. When you type `ls` in a shell, the shell forks a child process (which inherits the parent's file descriptor table), then execs `ls`, which writes its output to file descriptor 1 — your terminal.

I/O redirection exploits the fact that file descriptors are just integers that can be reassigned. When you write `ls > output.txt`, the shell forks, and before executing `ls`, the child process closes file descriptor 1 (stdout), then opens `output.txt` — which gets assigned file descriptor 1 because it is now the lowest available integer. When `ls` runs and writes to file descriptor 1, it writes to the file instead of the terminal, without `ls` knowing or caring that anything changed. The `dup2()` system call makes this even more explicit: `dup2(fd, 1)` copies whatever `fd` points to into slot 1, so stdout now refers to the same underlying file as `fd`.

Piping extends this pattern to connect two processes. When the shell encounters `ls | grep foo`, it creates a pipe (a kernel buffer with two file descriptors — one for reading, one for writing), forks two children, and wires the write end of the pipe to `ls`'s stdout and the read end to `grep`'s stdin. Each program reads and writes its standard descriptors as usual, unaware that they are connected to each other rather than to a terminal. This composability — programs that operate on abstract file descriptors rather than specific files — is the architectural insight that makes Unix pipelines possible.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → File Descriptor Tables and I/O Redirection

Longest path: 95 steps · 372 total prerequisite topics

Prerequisites (2)

Leads To (2)