fileno(stdin) versus STDIN_FILENO and friends

Peter Dufault dufault at
Sat Mar 12 12:54:04 UTC 2011

On Mar 11, 2011, at 3:26 , Eric Norum wrote:

> The document at 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?  Is that dictated by any standard?

I think it would make more sense if stdin, stdout, and stderr are always files 0, 1, and 2, 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,

and how this should play with pthreads? I haven't had much luck figuring it out.

Eric's earlier pointed-out-reference that STDIN_FILENO is by definition 0 implies to me that the way things are is at least confusing.

Peter Dufault
HD Associates, Inc.      Software and System Engineering

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the users mailing list