Open System Services Programmer's Guide

Example 65 Handling the SIGCHLD Signal
/* This program demonstrates the handling of the */
/* SIGCHLD signal */
/** You must also export the environment variable **/
/** SPT_THREAD_AWARE_SIGNAL to 1 to **/
/** enable thread-aware signal handling **/
#define SPT_THREAD_AWARE /** enables thread aware behavior **/
/** and maps functions to spt_ versions **/
#include <spthread.h>
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 =spt_sigaction(SIGCHLD,&sigact_t,NULL);
printf("\nspt_sigaction for thread1 returned %d\n",rc);
if ((pid = fork()) < 0)
printf("ERROR IN FORK\n");
else if (pid == 0)
{
sleep(2);
printf("\nInside Child of Thread1\n");
exit(0);
}
spt_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 = spt_sigaction(18,&osigact_t,NULL);
printf("\nspt_sigaction for thread2 returned %d\n",rc);
if ((pid = fork()) < 0)
printf("ERROR IN FORK\n");
else if (pid == 0)
{
sleep(1);
printf("\nInside Child of Thread2\n");
exit(0);
}
spt_waitpid(pid,NULL,NULL);
return NULL;
}
void main()
{
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,do_work1,NULL);
pthread_create(&tid2,NULL,do_work2,NULL);
sched_yield ();
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf ("\nMain Completed\n");
324 Using the Standard POSIX Threads Library