On 20/02/07, Peter <[EMAIL PROTECTED]> wrote:

> Huh?? When a group leader exits his parent gets SIGCHLD. That's one of
those
> sticky PITA's of UNIX process programming. (I've just double-checked
this
> with a quick program which demonstrate it). The only way I can remember
to
> avoid receving SIGCHLD is by setting it's handler to SIG_IGN (ignore).

A correctly written pgld has no parent. When it dies its exit signal
goes to init.


Care to demonstrate? The only way I'm aware of that a process' ppid may
change is when its direct parent dies and init adopts it. I don't see any
support for your claim in the documentation I found so far. I attached my
test case at the bottom of this message.

You are getting off subject - the original question was about how to find
> out that the other side of a socketpair has closed it.

There is none. That's the point. You can monitor the pid to see if it
went away. Worse if the process becomes a zombie then even that will not


Which pid? The child may very well have forked and exited so the parent is
actually talking to its grand-children, it could also be that multiple
processes are sharing the same socketpair on the other side (that's another
contingency I'll have to take care of).

Then again - if the socketpair() is "connected" (e.g. SOCK_STREAM or
SOCK_SEQPACKET) then the kernel will notify the parent as soon as the other
side was closed. Remember we are talking about Unix domain sockets.

--Amos

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

void reaper(int signal)
{
 int status;
 psignal(signal, "parent: signal");
 wait(&status);
 printf("child terminated with exit status %d signal %d\n",
        WIFEXITED(status) ? WEXITSTATUS(status) : -1,
        WIFSIGNALED(status) ? WTERMSIG(status) : -1);
 exit(0);
}

main ()
{
 printf("parent pgrp: %d\n", getpgrp());
 struct sigaction act = { .sa_handler = reaper, .sa_flags = 0, };
 if (sigaction(SIGCHLD, &act, NULL) == -1) {
   perror("sigaction(SIGCHLD)");
   exit(1);
 }
 int pid = fork();
 switch (pid) {
 case -1:
   perror("fork");
   exit(1);
 case 0:
   if (setpgrp() < 0) {
     perror("setpgrp");
     exit(1);
   }
   printf("child pgrp: %d; ppid: %d\n", getpgrp(), getppid());
   exit(0);
 default:
   if (setpgid(pid, pid) < 0) {
     perror("parent: setpgid");
     exit (1);
   }
   pause();
 }
}

Reply via email to