Open System Services Programmer's Guide

Example 88 Handling the SIGCHLD Signal
/* This program demonstrates the handling of the */
/* SIGCHLD signal */
#define _PUT_MODEL_ /** enables thread aware behavior **/
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include myio.h /** contains my_printf which is process-blocking**/
struct sigaction sigact_t,osigact_t;
sigset_t waitset;
int rc;
void sig_cld1(int);
void sig_cld2(int);
void *do_work1(void *arg)
{
pid_t pid;
sigact_t.sa_handler =sig_cld1;
sigact_t.sa_flags = 0;
rc =sigaction(SIGCHLD,&sigact_t,NULL);
my_printf("\nsigaction for thread1 returned %d\n",rc);
if ((pid = fork()) < 0)
my_printf("ERROR IN FORK\n");
else if (pid == 0)
{
sleep(2);
my_printf("\nInside Child of Thread1\n");
exit(0);
}
waitpid(pid,NULL,NULL);
return NULL;
}
void *do_work2(void *arg)
{
pid_t pid;
osigact_t.sa_handler =sig_cld2;
osigact_t.sa_flags = 0;
rc = sigaction(SIGCHLD,&osigact_t,NULL);
my_printf("\nsigaction for thread2 returned %d\n",rc);
if ((pid = fork()) < 0)
my_printf("ERROR IN FORK\n");
else if (pid == 0)
{
sleep(1);
my_printf("\nInside Child of Thread2\n");
exit(0);
}
waitpid(pid,NULL,NULL);
return NULL;
}
int main()
{
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,do_work1,NULL);
pthread_create(&tid2,NULL,do_work2,NULL);
sched_yield ();
Signals and Signal Handling 393