Waiting on parents and reinstating HUP

Jonathan I. Kamens jik at athena.mit.edu
Wed Apr 24 13:55:55 AEST 1991


In article <1991Apr23.144516.23119 at news.miami.edu>, aabenson at balance.cs.mtu.edu (Andrew A. Benson) writes:
|> Does anybody know how to set things up so a process started from csh
|> with an & will die when the user logs out?  Is there something I need
|> to do with SIGHUP signal handling?  I've tried everything I can think
|> of.  My only other thought is that it just never ever gets the signal.

  You're right, it never gets the signal.  Processes put into the background
in csh do not get a HUP signal when the shell exits.

|> My other question is if a process can check the status of its parent --
|> to check to see if the parent still exists.  All the calls I've found
|> only allow a process to check and/or wait on its children (the wait(),
|> wait3(), wait4(), and waitpid() calls under SunOS 4.1).

  In order to find out if your parent process still exists, you call getppid()
and see if the result is 1, i.e. the init process.  When a process's parent
exits, the process is inherited as a child by the init process.

  Therefore, if you are writing a program that is started in the background
from csh and you need it to notice when its parent csh has died and clean up
and exit, your program must periodically do getppid() and check the return
value.

  If your program is started by another program under your control, rather
than by csh, then you can create a pipe before the child is created; close the
read end of the pipe in the parent and the write end of the pipe in the child;
and then watch the pipe in the child to notice when it gets EOF (i.e. when the
parent exits and the pipe is closed).  You can watch the pipe either by using
periodic select() or poll() calls on it to check if there is data waiting to
be read (since that's how EOF is signalled), or by enabling asynchronous input
on the pipe and the delivery of SIGIO, and installing a SIGIO handler to
notice when the signal comes in.  The former will work will if your program is
already manipulating file descriptors using select() or poll().

-- 
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.programmer mailing list