fileno(stdin) versus STDIN_FILENO and friends

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


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:
> STDERR_FILENO
> File number of stderr; 2.
> STDIN_FILENO
> File number of stdin; 0.
> STDOUT_FILENO
> 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,
- STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO

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
-----------------
Peter Dufault
HD Associates, Inc.      Software and System Engineering

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20110312/8115b741/attachment-0001.html>


More information about the users mailing list