Actually, if your signals are being caught and handled, then the process isn't going to die, so there is no need to wait.
You only need waitpid IF the child is going to die.
Try this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#define SIG_PARENT_TO_CHILD SIGUSR1
#define SIG_CHILD_TO_PARENT SIGUSR2
volatile int p1 = 0;
void parent_catching_child ( int sig ) {
printf("Parent caught %d\n",sig);
p1++;
}
volatile int c1 = 0;
void child_catching_parent ( int sig ) {
printf("Child caught %d\n",sig);
c1++;
}
void doChild ( void ) {
signal(SIG_PARENT_TO_CHILD,child_catching_parent);
sleep(1);
while ( !c1 ) {
sleep(1);
}
kill(getppid(),SIG_CHILD_TO_PARENT);
}
void doParent ( pid_t childPid ) {
signal(SIG_CHILD_TO_PARENT,parent_catching_child);
sleep(1);
kill(childPid,SIG_PARENT_TO_CHILD);
while( !p1 ) {
sleep(1);
}
int status;
waitpid(childPid,&status,0);
if ( WIFEXITED(status) ) {
int ret = WEXITSTATUS(status);
printf("Child exited normally with %d\n",ret);
} else
if ( WIFSIGNALED(status) ) {
int sig = WTERMSIG(status);
printf("Child exited abnormally with signal %d\n",sig);
} else {
fprintf(stderr,"Oops\n");
}
}
int main ( ) {
pid_t pid;
if ( (pid=fork()) == 0 ) {
doChild();
} else {
doParent(pid);
}
return 0;
}