No history yet

Introduction to File Descriptors

The Coat Check System

In Unix-like operating systems, almost everything is treated as a file. A document is a file, a directory is a file, and even your keyboard and screen are treated like files. When a program wants to read from or write to one of these, it doesn't use the file's name. Instead, it asks the operating system to open the file.

In return, the operating system gives the program a simple, non-negative integer. This number is called a file descriptor.

Think of it like a coat check. You hand over your coat (the file), and you get a ticket with a number on it. When you need your coat back, you don't describe it; you just show your ticket number. The file descriptor is that ticket number.

This system simplifies things for the program. It no longer needs to know if it's writing to a document, a printer, or a network connection. It just tells the operating system, "Hey, send this data to whatever file descriptor 1 is pointing to."

The Standard Three

Whenever you run a program in a terminal, the operating system automatically opens three standard I/O streams for it. Each one comes with a pre-assigned file descriptor number.

NumberNameAbbreviationDefault Purpose
0Standard InputstdinInput from the keyboard
1Standard OutputstdoutNormal output to the screen
2Standard ErrorstderrError messages to the screen

Let's break this down.

Standard Input (stdin), file descriptor 0, is the program's default source of information. When a program waits for you to type something, it's typically reading from stdin.

Standard Output (stdout), file descriptor 1, is where a program sends its normal results. The text you see after running a command like ls is sent to stdout, which by default is your terminal screen.

Standard Error (stderr), file descriptor 2, is a separate channel reserved for error messages. This is useful because it allows you to separate a program's successful output from its error messages. Like stdout, it also points to your screen by default.

Lesson image

How It All Connects

Each running program, or process, gets its own private list of file descriptors. This list is called the file descriptor table. The operating system maintains this table for each process, tracking which file or device each number points to.

Notice that file descriptor numbers are unique only within a single process. Process A can have a file descriptor 3 pointing to data.txt, while Process B's file descriptor 3 points somewhere else entirely. However, both processes start with file descriptors 0, 1, and 2 pointing to the same keyboard and screen.

This simple integer-based system is a core part of what makes Unix-like environments so flexible. It provides a consistent way for programs to handle input and output, regardless of where that I/O is coming from or going to.

Quiz Questions 1/5

In a Unix-like operating system, what is a file descriptor?

Quiz Questions 2/5

When a program running in a terminal needs to display an error message, which standard file descriptor should it use by default?