[comp.sys.hp] Why can't I detect when the child dies? help.

garvey@cmic.UUCP (Joe Garvey) (02/17/90)

I'm having a devil of a time with the following scrap of code. It will fork
the child correctly, do all the I/O fine. It's just that the wait from the
parent is never statisfied. The child becomes a zombie process, and won't
go away until the parent is killed. All suggestions greatly appreciated.
E-mail preferred.

PS. I neglected to mention this program works like nice and nohup. The
child program is based on the arguments to the parent command. The name of
my program is "backcon".

nohup sleep 10    #yes I know this is dumb, aren't all trivial examples? :-)
nice sleep 10
backcon sleep 10      #example 1
backcon rm testfile   #example 2

PPS. I'm running HP-UX 6.5 on a HP9000/370. It's a SYSV system with lotsa
BSD features (like the fast file system).


Peoples Exhibit #1: The Offending segment of code.

Mstr_Pty_fd = Open_Pty_Master(Slv_Pty_Name);

if ((Child_Pid = fork()) < 0) then                  /* trap out a failed fork */
   {
   exit(FORK_FAILED);
   }
end_if

if (Child_Pid == 0)                                                  /* child */
   {
   setpgrp();                           /* start a new terminal process group */

   Slv_Pty_fd = Open_Pty_Slave(Slv_Pty_Name);

   close(Mstr_Pty_fd);              /* don't need master side file descriptor */

   close(0);      /* close stdin, stdout, and stderr since they go to old tty */
   close(1);
   close(2);

   dup(Slv_Pty_fd);              /* open new stdin, stdout, stderr to new tty */
   dup(Slv_Pty_fd);
   dup(Slv_Pty_fd);

   close(Slv_Pty_fd);       /* don't need any more, got stdin, stderr, stdout */

                              /* child becomes user program from command line */
   execvp(ArgV[Cmd_Line.Child_Program], ArgV+Cmd_Line.Child_Program);
   }
else                                                                /* parent */
   {
   wait(NULL);
   }
/* end_if */

--
Peoples Exhibit #2: Author of Offending Segment of Code.

Joe Garvey                       UUCP: {apple,backbone}!versatc!mips!cmic!garvey
California Microwave             Internet: garvey%cmic@mips.com
990 Almanor Ave                  HP Desk: garvey (cmic@mips.com) /hp1900/ux
Sunnyvale, Ca, 94086             800-831-3104 (outside CA)
408-720-6439 (let it ring)       800-824-7814 (inside CA)

stroyan@hpfcso.HP.COM (Mike Stroyan) (02/19/90)

> I'm having a devil of a time with the following scrap of code. It will fork
> the child correctly, do all the I/O fine. It's just that the wait from the
> parent is never statisfied. The child becomes a zombie process, and won't
> go away until the parent is killed. All suggestions greatly appreciated.

The parent in your example has opened the master side of pty, while the
child has opened the slave side of the same pty.  If you have asked for
TIOCTRAP on the pty, then a close on the slave side will block until the
master side handshakes it.  When the child tries to exit, it will attempt
to close the pty on the way out.  Until the parent handshakes the close
the child will not completely exit, and the wait() for the child will
not complete.  Perhaps you could watch for a close operation before doing
a wait() or waitpid() for the corresponding process.

Mike Stroyan, stroyan@hpfcla.hp.com