Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. file-descriptors
7 min read

File Descriptors

Underneath every file you open, every network connection you make, and every pipe between two programs, Linux hands your program the same simple thing: a file descriptor. Getting comfortable with file descriptors explains something that surprises most newcomers — reading from a file and reading from a network socket use the exact same code — and it is the layer that the friendlier FILE * functions in the next lesson are built on top of.

What a File Descriptor Is

A file descriptor (an fd) is just a small non-negative integer. The kernel keeps a table, one per process, of everything that process currently has open, and the fd is simply the index into that table. When you want the kernel to act on an open thing, you hand back its number.

file descriptor table 0stdin — the terminal 1stdout — the terminal 2stderr — the terminal 3a file on disk 4

When you open something, the kernel gives you back the lowest unused number. Numbers 0, 1, and 2 are already taken when your program starts, so the first thing you open is usually 3.

Everything Is a File

Look again at that table. Descriptor 3 is a file, descriptor 4 is a network socket — and yet the kernel hands you the same kind of integer for both, because on Linux almost everything you can read from or write to is presented through the same file interface. Regular files, pipes between programs, network sockets, your terminal, even devices like your keyboard and disk — all of them are reached through a file descriptor, and all of them respond to the same handful of operations. This design principle has a name: "everything is a file."

It is not just a slogan — you can see the table directly. Linux exposes each process's open descriptors under /proc/self/fd, where every fd is a symbolic link to whatever it actually points at:

$ ls -l /proc/self/fdlrwx------ 1 user user 64 Nov 18 22:06 0 -> /dev/pts/11lrwx------ 1 user user 64 Nov 18 22:06 1 -> /dev/pts/11lrwx------ 1 user user 64 Nov 18 22:06 2 -> /dev/pts/11lr-x------ 1 user user 64 Nov 18 22:06 3 -> /proc/3133396/fd

Descriptors 0, 1, and 2 link to the terminal device; the one ls opened to read the directory is a few numbers higher. The payoff is practical: because a socket is reached through a descriptor just like a file, the read() and write() you learn here are the same calls you will use to talk over the network in the client/server project. There is no separate "socket read" — it is all just reading and writing descriptors.

stdin, stdout, stderr

The three descriptors every program starts with are the standard streams:

  • 0 — standard input (stdin), where input comes from.
  • 1 — standard output (stdout), where normal output goes.
  • 2 — standard error (stderr), where error messages go, kept separate so they can be redirected on their own.

When you call printf, it ultimately writes to descriptor 1. When you read from the keyboard, it comes from descriptor 0.

The Raw Syscalls

Below the friendly library functions sit the system calls that operate on descriptors directly: open returns a new fd, read and write move bytes through one, and close releases it. Here they are in action — notice that the very same write call sends bytes to the terminal and to a file, just by changing which descriptor we hand it:

fds.c
1#include <fcntl.h>
2#include <stdio.h>
3#include <string.h>
4
$ gcc fds.c -o fds$ ./fdsopen("note.txt") returned fd 3same write(), two destinations$ cat note.txtsame write(), two destinations

open handed back 3 — the first free slot after the standard streams — and the identical write worked on descriptor 1 and descriptor 3 without caring what either one was. That is "everything is a file" in a single program.

Redirection with dup2

Because a descriptor is just a slot in a table, you can make one slot point at what another one points at. dup2(fd, 1) copies fd onto descriptor 1, so everything written to "stdout" now actually goes wherever fd leads. Point it at a file and your program's output lands in the file, with no change to the code that prints:

dup2.c
1#include <fcntl.h>
2#include <stdio.h>
3#include <unistd.h>
4
5
$ gcc dup2.c -o dup2$ ./dup2$ cat out.txtthis line went to the file, not the terminal

The program printed nothing to the terminal — printf wrote to descriptor 1, which we had quietly redirected into out.txt. This is exactly the mechanism behind the shell's > operator: ./myprog > out.txt is the shell doing this dup2 for you before it runs your program.

FILE* Is a Wrapper

So where do fopen and printf fit in? The FILE * you get from fopen is a wrapper around a raw descriptor. It holds the fd number plus a buffer, so that instead of making a system call for every byte, your program fills a buffer and the library flushes it in big, efficient chunks. You can always find the descriptor underneath with fileno(fp).

That buffering, and the convenient reading and parsing functions built on top of it, are what the File I/O lesson is about — now that you know it is all descriptors underneath.

a network socket
#
include
<unistd.h>
5
6int main(void)
7{
8 int fd = open("note.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
9 printf("open(\"note.txt\") returned fd %d\n", fd);
10
11 char *msg = "same write(), two destinations\n";
12 write(1, msg, strlen(msg)); // fd 1 — the terminal
13 write(fd, msg, strlen(msg)); // fd 3 — the file
14
15 close(fd);
16 return 0;
17}
int
main
(
void
)
6{
7 int fd = open("out.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
8 dup2(fd, 1); // make descriptor 1 (stdout) point at out.txt
9 close(fd);
10
11 printf("this line went to the file, not the terminal\n");
12 return 0;
13}