fork question

Jonathan I. Kamens jik at athena.mit.edu
Thu Feb 7 07:46:37 AEST 1991


  When you run fork(), the child process that results from the fork() has the
same open file descriptors (except for the ones that are set to close-on-exec)
as the parent process.

  When you use back-quote command evaluation in the shell, the shell keeps
reading on the stdout file descriptor of the process it calls, until it gets
EOF on that file descriptor.

  The shell won't get EOF on the file descriptor until *all* processes that
have the other end of the file descriptor open have closed it (or exited and
implicitly closed it).

  Therefore, although your parent process exits, the shell continues to wait
for output, because the child process still has stdout open, so the shell does
not get an EOF.

  The easiest way to fix this is to close stdout and reopen /dev/null or
something on top of it in the child process.  If you need to output stuff to
stdout in the child, you'll have to come up with something more creative, like
opening /dev/tty for write after closing stdout, or dup'ing stderr's file
descriptor onto stdout, or something like that.  Deciding exactly how to get
back a stdout to write to depends on exactly what your program does.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.questions mailing list