Definition :
- In Unix systems, a file descriptor is or a small positive integer used as reference to an open file in a process
- A process is a currently running program.
- From the operating system’s POV, a file is not only a text file, a file can also be a directory or even another type of input/output resource.
Why using a file descriptor ?
- An integer is much more simpler than a long path to process for a computer, also the reference to a file must contain much more than its location, its must include permissions, access mode, size …
System representation of open files :

The offset :
- the offset represent the current number of bytes from the beginning of the file, which gives us the current position in the file. This is what the read function increments at the end of its execution.
- Adjusting the file descriptors offset :
- Resetting the offset with a new file descriptor :
- Open the same file again, this creates a new entry in the system’s open file table, with an offset of 0 by default.
- Using lseek :
- off_t lseek (int fd, off_t offset, int whence);
- Duplicating the file descriptor :
- it can be useful to duplicate a fd in order to save it as backup or replace another one.
- the dup and dup2 sys calls enable us to do such thing :
int dup (int oldfd);
int dup2 (int oldfd, int newfd);
- dup : automatically chooses the smallest unused fd.
- dup2 : we can specify which number we want.