πŸ—ƒοΈFile descriptors (FD)

I'll try to explain what is a file descriptor is since it's used in most projects

What is a File Descriptor ?

A file descriptor is an int variable that uniquely identifies an open file.

Terminology

Before going further with the explanation, I have to describe some terms that I will use on this page.

Term
Description

File Descriptor

This is the index of the File Table Entry in the file descriptor table.

File Descriptor Table

This is an array of File Table Entry, each process gets its own File Descriptor Table.

File Table Entry

A File Table Entry is a structure that contains informations about a file.

Global File Table

This is a system wide table containing all files. (It can't contain all files at once but the operating system will automatically update the table if you request a file that's not in this table).

When you use a file descriptor, with the read(2) function for example, the following will happen :

  1. Search for the file in the Global file table

    1. If the file is found, go to next step.

    2. If the file is not found, the operating system will update the Global file table to make the requested file available, then go back to step 1.

  2. Create a File table entry in the File descriptor table for the requested file.

  3. Assign the first unused File descriptor to the created file table entry.

// insert schema here

Standard file descriptors

In C, like in most Unix systems, there are 3 standards file descriptors that are automatically added to the file descriptor table. These file descriptors are the standard input/output file descriptor and always have these values :

  • 0 : this file descriptor represents the stdin (=> standard input, the terminal). This is the file descriptor used when reading user input from the terminal.

  • 1 : this file descriptor represents the stdout (=> standard output, the terminal). This is the file descriptor used when writing to the terminal.

  • 2 : this file descriptor represents the stderr (=> standard error output, the terminal). This is the file descriptor used when writing an error to the terminal, the information is written the same way, but a program which logs errors to a file can redirect everything written to the stderr file descriptor to a file. It's managed in another way by the operating system.

Examples

When you use the write(2) function, you're actually using a file descriptor.

The prototype of the write(2) function is as follows :

As you can see, the first parameter to this function is an int called fd, sounds familiar right ?

You can specify in which file to write with the first parameter, I'll make a quick example to show you how it could be used.

How it works

When you read bytes from a file descriptor, it remembers where in the file it was last time.

This means, if you read 20 bytes from a file, next time you'll read from the same file descriptor, it will start reading from byte 21. Take a look at the example below.

Last updated

Was this helpful?