Skip to content
systemcoding.dev
Systems Programming

File Descriptors vs Open File Descriptions vs Inodes

A file descriptor points to an open file description, which points to an inode. See which layer holds the offset, flags and data, with dup and fork demos.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Linux, C++ and GNU logos.

Three words get used as if they meant the same thing: file descriptor, open file description and inode. They are three separate layers, and most puzzling file I/O behavior comes from mixing them up. Why does a child process that reads from an inherited descriptor move the parent's read position? Why does opening the same path twice give you two independent positions? Why does deleting a file not free its disk space while a program still has it open? Each answer lives at a different layer. This post names the layers, shows which one owns the offset, the flags and the data, and proves each claim with a short program. If a descriptor is still a new idea, read what a file descriptor is first.

Three layers, three owners

Picture a chain of three kernel structures, each pointing to the next.

The first layer is the file descriptor: a small integer in your process's descriptor table. Each slot in that table holds a pointer to an open file description, plus one flag of its own, the close-on-exec flag FD_CLOEXEC. That flag belongs to the descriptor, not to the file. A process owns this table, and fork() copies it.

The second layer is the open file description, which POSIX calls an open file description and Linux implements as struct file. It records the current file offset, the access mode (O_RDONLY, O_WRONLY or O_RDWR) and the status flags such as O_APPEND and O_NONBLOCK. It is created by open() and shared, not copied, when a descriptor is duplicated with dup() or inherited through fork().

The third layer is the inode: the filesystem's record of the file itself. It holds the size, the permissions, the owner, the timestamps, and the pointers to the data blocks. Many open file descriptions can point to the same inode, and the inode persists as long as it has either a directory entry (a link) or an open file description referring to it.

The shortest summary is a table of who holds what:

LayerCreated byHoldsShared when
File descriptoropen(), dup(), socket()pointer to a description, FD_CLOEXECnever; one per slot in one process
Open file descriptionopen()offset, access mode, status flagsdup() or fork() copies the descriptor
Inodecreating the filesize, mode, owner, dataevery open() of the same file

Same description: shared offset

When two descriptors point at the same open file description, they share the offset. dup() is the simplest way to make that happen:

shared_offset.cc
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int fd = open("letters.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
    write(fd, "abcdefghij", 10);
    lseek(fd, 0, SEEK_SET);

    int copy = dup(fd);          /* second descriptor, same description */

    char a, b;
    read(fd, &a, 1);             /* reads 'a', offset becomes 1 */
    read(copy, &b, 1);           /* reads 'b', not 'a' */
    printf("fd read '%c', copy read '%c'\n", a, b);

    printf("offset via fd: %ld\n", (long)lseek(fd, 0, SEEK_CUR));
    close(copy);
    close(fd);
    return 0;
}

Built with gcc -Wall -o shared_offset shared_offset.c on GCC 14, the program prints:

Text
fd read 'a', copy read 'b'
offset via fd: 2

The second read continued where the first stopped. copy did not get its own position: reading through it advanced the offset that fd sees, because both slots point at one description. The same sharing applies to the status flags, so setting O_NONBLOCK on one of them with fcntl(F_SETFL) changes the other.

Separate descriptions: independent offsets

Opening the same path a second time creates a new open file description, even though the inode is the same. Each description has its own offset:

separate_offsets.cc
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int first = open("letters.txt", O_RDONLY);
    int second = open("letters.txt", O_RDONLY);

    char c1, c2, c3;
    read(first, &c1, 1);         /* first: offset 1 */
    read(first, &c2, 1);         /* first: offset 2 */
    read(second, &c3, 1);        /* second starts at 0 */

    printf("first read '%c' then '%c'; second read '%c'\n", c1, c2, c3);
    close(first);
    close(second);
    return 0;
}

With letters.txt still containing abcdefghij from the previous program, the output is:

Text
first read 'a' then 'b'; second read 'a'

Two descriptors, two descriptions, one inode. The data came from the same file, but the reading positions were independent. This is why two threads that each open() a log file get separate positions, and why they would overwrite each other's lines without O_APPEND: each description tracks its own offset and neither knows about the other's writes.

fork shares the description too

After fork() the child holds a copy of the descriptor table, and each copied slot points at the same open file description. The practical consequence is that a parent and a child that read from an inherited descriptor take turns on one offset:

fork_offset.cc
#include <fcntl.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    int fd = open("letters.txt", O_RDONLY);

    if (fork() == 0) {
        char c;
        read(fd, &c, 1);
        printf("child read '%c'\n", c);
        _exit(0);
    }

    wait(NULL);
    char c;
    read(fd, &c, 1);
    printf("parent read '%c'\n", c);
    return 0;
}

The child reads first because the parent waits, so the output is:

Text
child read 'a'
parent read 'b'

The parent did not read a again. The child's read() advanced the shared offset, and the parent continued from there. This is by design: it is how a shell passes one input stream to a sequence of commands, and it is a classic source of "who consumed my input" bugs when two processes read the same descriptor without coordination.

The inode outlives the name

The third layer explains deletion. unlink() removes a directory entry, which decrements the inode's link count. The inode, and its data, stay until the link count is zero and no open file description refers to it. A program that opens a file and then unlink()s it can keep reading and writing it, and the disk space comes back only when the last description closes.

unlinked.cc
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int fd = open("scratch.tmp", O_RDWR | O_CREAT | O_TRUNC, 0600);
    unlink("scratch.tmp");                     /* the name is gone */

    write(fd, "still here", 10);
    lseek(fd, 0, SEEK_SET);

    char buf[16] = {0};
    read(fd, buf, sizeof buf - 1);
    printf("read back: %s\n", buf);
    close(fd);                                 /* space released now */
    return 0;
}
Text
read back: still here

That pattern is a standard way to make an anonymous temporary file that cannot be left behind by a crash. It also explains why df can show a full disk after you delete a big log: a daemon still holds a descriptor open, so the inode survives. Restarting the daemon, or truncating the file through /proc/<pid>/fd/<n>, releases it.

Where each layer shows up in tools

You can see the layers from the shell on Linux. ls -l /proc/self/fd lists the descriptor layer, showing each number and the path it resolves to. The file /proc/<pid>/fdinfo/<n> shows the open file description's pos and flags. And stat or ls -i shows the inode number. Two descriptors that share a description show identical pos values in fdinfo, which is a handy way to confirm the sharing when debugging.

Why logs care about the difference

The split between description and inode explains a well-known logging rule. Two processes that each open() the same log file get two descriptions with two offsets. Process A writes 100 bytes at offset 0, process B also writes at its own offset 0, and B's text overwrites A's. Nothing in the inode coordinates them, because the offset is not stored there.

O_APPEND fixes this at the description layer. It is a status flag of the open file description, and with it set the kernel moves the offset to the end of the file as part of each write(), as one step. Two descriptions both opened with O_APPEND can then write to one inode without clobbering each other, as long as each line goes out in a single write() call. A printf() that the C library splits across two write() calls can still interleave with another process, so build the whole line first and write it once.

A shared description through fork() behaves differently again. Parent and child with one description never overwrite each other, because there is only one offset and every write advances it. They can still interleave their output in an unpredictable order, since neither knows when the other will write.

append_log.cc
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    int fd = open("app.log", O_WRONLY | O_CREAT | O_APPEND, 0644);
    const char *line = "worker started\n";
    write(fd, line, strlen(line));      /* one call, one atomic append */
    close(fd);
    return 0;
}

Run twice, the file ends up with both lines, because each run opened a fresh description and O_APPEND positioned each write at the end:

Text
worker started
worker started

When the model has limits

The three-layer picture is the POSIX and Linux model. Not every descriptor names an inode on a disk: a pipe has an inode in the pipe filesystem, a socket has one in sockfs, and epoll and eventfd objects are anonymous inodes. The layers are still there, but the inode holds no file data in the filesystem sense. Network filesystems also add a wrinkle, since the offset is client-side state and the inode's size and timestamps are cached copies of what the server holds.

Share

Keep reading

Flat illustration of two options weighed side by side, with the macOS, Linux and Apple logos.
Kernels

Mach Traps vs BSD System Calls on macOS

Learn how macOS routes one trap instruction to either a Mach trap or a BSD system call, how the number sign and class decide, and why you should call libSystem.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.