A file descriptor is a number your operating system uses to keep track of open files
When you open a file — whether you click it in a folder, a program reads it, or a background task accesses it — your operating system doesn't hand the file itself to the program. Instead, it hands the program a number. That number is the file descriptor. It's a reference, a ticket, a way for the program to say "I want to read from the thing I opened earlier" without having to remember the full file path or location.
File descriptors are small integers, usually starting at 0 and going up. Your operating system keeps a table that matches each number to the actual file, where it is, and what the program is allowed to do with it. When a program wants to read data, write data, or close a file, it uses the descriptor number. The operating system looks up that number in its table and performs the action on the real file.
This system exists because programs shouldn't have direct access to files. The operating system sits in the middle as a gatekeeper. It can enforce permissions, prevent two programs from corrupting the same file at the same time, and keep track of what's open so nothing gets lost when a program crashes.
Key Takeaways
- A file descriptor is a number assigned by your operating system that represents an open file, allowing programs to reference files without knowing their full paths.
- Every program has a table of file descriptors starting with 0 (standard input), 1 (standard output), and 2 (standard error) already reserved.
- When a program opens a file, the operating system assigns the next available descriptor number and keeps track of which file it points to.
- File descriptors are closed when a program finishes with a file, freeing up the number for the operating system to reuse.
How file descriptors get assigned
When a program starts, the operating system automatically creates three file descriptors for it: 0, 1, and 2. These are standard and always the same. Descriptor 0 is standard input — where the program reads keyboard input or piped data. Descriptor 1 is standard output — where the program sends normal results. Descriptor 2 is standard error — where the program sends error messages. These three exist whether the program uses them or not.
When a program opens a new file — by clicking it, loading it through a menu, or through code — the operating system assigns the next available number, usually 3. If the program opens another file, it gets 4. If the program closes the file with descriptor 3, that number goes back into the pool and can be reused for the next file opened. This is why the numbers don't always go in order on a running system.
The operating system limits how many file descriptors a single program can have open at once. On most systems, the limit is somewhere between 256 and 1024, though it can be changed. If a program tries to open more files than the limit allows, it will fail with an error message like "too many open files."
Why the operating system uses this system
File descriptors create a layer of control. A program cannot directly access a file on disk — it must ask the operating system to do it. This prevents a buggy or malicious program from reading files it shouldn't have permission to read, writing to files it shouldn't touch, or corrupting data by accessing the same file in conflicting ways.
The descriptor system also makes it possible for the operating system to track what's open. If a program crashes, the operating system can see which files it had open and close them automatically. Without this tracking, files could be left in a corrupted state or locked permanently. The operating system can also enforce rules like "only one program can write to this file at a time" by checking the descriptor table before allowing an operation.
From a program's perspective, file descriptors are simpler than file paths. A program doesn't need to remember that a file is at "/Users/yourname/Documents/report.txt" every time it wants to read a line. It just says "read from descriptor 5" and the operating system handles the rest.
What happens when you close a file
When a program finishes with a file and closes it, the operating system removes that file descriptor from the program's table. The number becomes available for reuse. The file itself is not deleted — closing a file descriptor just means the program is no longer reading from or writing to it.
If a program forgets to close a file, the descriptor stays open and the file stays locked. This is called a file descriptor leak. If a program leaks enough descriptors, it will eventually hit the system limit and won't be able to open any more files. This is a common cause of programs that seem to freeze or crash after running for a while — they've run out of available descriptors because old ones were never closed.
Most programs close files automatically when they're done, but some poorly written programs don't. If you notice a program slowing down or refusing to open new files after it's been running for a while, a file descriptor leak might be the cause.
File descriptors in different operating systems
The concept of file descriptors is the same across Windows, macOS, and Linux, but the names and some details differ. On macOS and Linux, file descriptors work exactly as described above. On Windows, the system uses a similar concept called handles, which work the same way but have a different internal structure and are managed slightly differently.
For most users, the differences don't matter. Whether you're on Windows, macOS, or Linux, the operating system is managing file access through a numbering system, and programs are using those numbers to read and write files safely. The details of how the system works under the hood are hidden from you.
When you might encounter file descriptor limits
Most of the time, file descriptors work silently in the background and you never think about them. But in certain situations, you might run into the limits. If you're running a server that handles many simultaneous connections, each connection might use a file descriptor. A web server handling thousands of requests at once could run out of descriptors if the limit is set too low.
Database programs, backup software, and media players that work with large libraries can also hit descriptor limits. If you see an error message mentioning "too many open files" or "file descriptor limit," the program has tried to open more files than the system allows. System administrators can increase the limit, but it's usually a sign that something is opening more files than it should.
For everyday computer use — browsing the web, editing documents, watching videos — you will never encounter this limit. It's only a concern for programs that manage many files simultaneously or for servers handling high traffic.
Frequently Asked Questions
Can I see what file descriptors a program has open?
On macOS and Linux, you can use the lsof command in the terminal to list open files and their descriptors. On Windows, you can use the Resource Monitor or Process Explorer to see what files a program has open. These tools are mainly useful for troubleshooting, not for everyday use.
What happens if a program crashes without closing its files?
The operating system automatically closes all file descriptors belonging to a crashed program. The files themselves are not deleted, but any unsaved data in memory is lost. This is why you sometimes see "recover unsaved changes" prompts when a program restarts.
Is there a difference between closing a file and deleting it?
Yes. Closing a file descriptor tells the operating system you're done using the file. Deleting a file removes it from the file system entirely. You can close a file without deleting it, and you can delete a file that's currently open (though the operating system may prevent this depending on permissions).
Why do file descriptors start at 0 instead of 1?
This is a convention from early Unix systems. Programmers found it convenient to start counting at 0, and the standard input, output, and error streams were assigned to 0, 1, and 2. The convention stuck and is now universal across operating systems.
Can a program run out of file descriptors while I'm using it normally?
Unlikely. Normal programs like web browsers, email clients, and word processors close files as soon as they're done with them. You would only hit the limit with programs that intentionally work with many files at once, or with programs that have a bug causing them to leak descriptors.