3 ms·
The kernel gives you the lowest numbered fd available. If you close stderr, 2 becomes available and can be handed out. Also it's not exactly guaranteed that 0
by emilfihlman 7y ago
The kernel gives you the lowest numbered fd available.
If you close stderr, 2 becomes available and can be handed out.
Also it's not exactly guaranteed that 0, 1 and 2 are the fd numbers for stdin, out and err on program start, but they are hard coded and standard.
E: It's implementation defined, ie not guaranteed. Your platform may make guarantees about it, but it is not an actual standard.
- matheusmoreira 7y agohttp://man7.org/linux/man-pages/man3/stdin.3.html http://man7.org/linux/man-pages/man3/stdin.3.html > On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively.
- emilfihlman 7y agoYou must differentiate between a guarantee by an actual standard and platform specific documentation.
- matheusmoreira 7y agohttps://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/s... > The following symbolic values in <unistd.h> define the file descriptors that shall be associated with the C-language stdin, stdout, and stderr when the application is started: > STDIN_FILENO Standard input value, stdin. Its value is 0. > STDOUT_FILENO Standard output value, stdout. Its value is 1. > STDERR_FILENO Standard error value, stderr. Its value is 2.
- deleted 7y ago[deleted]
- quindecagon 7y ago> On program startup Then, after program startup, if it is closed, then fd 2 could be assigned to something else.
- blattimwind 7y ago> Also it's not exactly guaranteed that 0, 1 and 2 are the fd numbers for stdin, out and err on program start Yes, it is. https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/s...
- emilfihlman 7y agoYou must differentiate between a guarantee by an actual standard and platform specific documentation.
- endorphone 7y agoHe linked an actual standard.
- blattimwind 7y agoCan you tell me what the "std" in "IEEE Std 1003.1-2017" stands for?
- makomk 7y agoThat is a guarantee by an actual standard. More specifically, it's one of the things that POSIX.1-2017 guarantees but the ISO C standard does not. In practical terms this means that all Unix-like systems use the traditional fd numbers for stdin, stdout and stderr but it's possible that non-Unix systems might not.
- caf 7y ago(Standard C doesn't have file descriptors at all, it has FILE * file handles).
- trasz 7y agoYou’re right, but it’s more than that: stderr is defined to be fd 2. Whatever is assigned to that fd, is the stderr. If you close it and open something else, that something becomes your stderr.
- Hello71 7y agoIt is not only guaranteed by the POSIX standard, as pointed out by the siblings, it is necessary for standard shell scripting idioms to work, e.g. "command1 2>&1 | command2".