Search code examples
clinuxassemblyx86-64file-descriptor

What is the size in bits of a file descriptor in an x64 system running Ubuntu 19.10?


What is the size in bits of a file descriptor such as standard input and standard output, is it a 32-bit integer?


Solution

  • If you are talking about the actual file descriptors returned by (and used for) Linux syscalls, then take a look at the manpage for open etc. as @JonathanLeffler suggests.

    For instance:

    int open(const char *pathname, int flags);
    

    The return value of open() is a file descriptor, a small, nonnegative integer that is used in subsequent system calls. [...] The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.

    Given that Unix-like systems are LP64, int and therefore the file descriptors are 32-bit wide.

    However, note that the kernel will give you as small integers as possible and that you will typically reach a limit way before that (see Limits on the number of file descriptors) either due to the kernel global limit or the soft/hard limits.

    This means that, if you really needed it, you could in theory use a smaller integer to store your file descriptors, e.g. an int16_t or an int8_t (assuming that your process does not use that many file descriptors at a time).


    If, instead, you are referring to stdin etc., those are not file descriptors but file streams defined by the C standard.

    They are macros which expand to expressions with pointer type (FILE *), and pointers in a typical 64-bit platform like x86_64 are 64-bit wide.

    See 7.21p3 (Input/output <stdio.h>):

    stdin
    stdout
    stderr
    

    which are expressions of type ‘‘pointer to FILE’’ that point to the FILE objects associated, respectively, with the standard error, input, and output streams.