fileno(stdin) versus STDIN_FILENO and friends
ralf.corsepius at rtems.org
Sat Mar 12 17:02:15 UTC 2011
On 03/12/2011 01:54 PM, Peter Dufault wrote:
> On Mar 11, 2011, at 3:26 , Eric Norum wrote:
>> The document at http://pubs.opengroup.org/onlinepubs/009695399/basedefs/unistd.h.html implies that they really are supposed to be constants:
>> The following symbolic constants shall be defined for file streams:
>> File number of stderr; 2.
>> File number of stdin; 0.
>> File number of stdout; 1.
>> but I agree that that is rather counterintuitive.
> Am I correct that threads are started with the streams stdin, stdout, and stderr set to files 0, 1, and 2,
> and don't inherit the file descriptors that the parent thread may have redirected them to?
The are global FILE*'s which are supposed to default to fileno's
0, 1, 2.
> Is that dictated by any standard?
see the link below.
> I think it would make more sense if stdin, stdout, and stderr are always files 0, 1, and 2,
This is not safe to assume. stdin, stdout and stderr are FILE*'s and can
be redirected, repopened or even be closed.
> that the underlying files are switched at context switch time, and that threads inherit whatever underlying files the creating thread may have dup'd them to. Next in line would be to inherit the parent file descriptors.
> Does anyone know about what the standards say about:
> - stdin, stdout, stderr,
> - file descriptors 0, 1, 2,
> - STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
> and how this should play with pthreads?
FILE* are per-process and not per-thread.
> I haven't had much luck figuring it out.
The best I could find is this:
More information about the users