Reopening stdin

Chuck Musciano chuck at trantor.harris-atd.com
Tue Jan 8 04:46:13 AEST 1991


     Environment: Sun-3/60 running SunOS 4.1
     
     I have a program which at one point forks itself.  The child, in turn,
calls system() to run a command.  In the child, I would like to reopen stdin
to point to a particular file.  To do this, I call open() and dup2() before
calling system().

     Here is the strange behavior.  If the parent has at some point closed
its stdin, the command that the child invokes with system() terminates
with "stdin: bad file number".  If the parent does not close its stdin, all
works fine.  The close of stdin occurs before the child is forked.

     My question: what unusual behavior occurs when you close stdin such
that your children cannot successfully dup2() into it and fork again (via
system()).  The trivial solution is to never close stdin in the parent, but
my curiosity has been piqued by this odd behavior.  Any insight on this?

-- 

Chuck Musciano				ARPA  : chuck at trantor.harris-atd.com
Harris Corporation 			Usenet: ...!uunet!x102a!trantor!chuck
PO Box 37, MS 3A/1912			AT&T  : (407) 727-6131
Melbourne, FL 32902			FAX   : (407) 729-2537

A good newspaper is never good enough,
	but a lousy newspaper is a joy forever.		-- Garrison Keillor



More information about the Comp.unix.questions mailing list