[comp.lang.c] [REQUEST] Need help catching SIGCHLD

ries@venice.SEDD.TRW.COM (Marc Ries) (05/02/91)

Could some kind soul clue me in to either the right way to do the 
following or tell me what I'm doing wrong? Thanks Marc ries@venice.sedd.trw.com

Basically, I am just tring to learn how to do a fork and exec
a child process, return to the main (which is free to do other
things in the meantime) and let the main be notified when
the child dies.

The following quickie test code doesn't work:
("childprog" is something simple like: main(){sleep(15);return;})

#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>

#define CHILD 0

int             pid;
main()
{
	extern void     exec_child();
	exec_child();
	sleep(60);
	return (0);
}

void
exec_child()
{
	char           *program = "childprog\0";
	int             catch_sigchld();
	static int      first_time;

	if (first_time) {
		(void) signal(SIGCHLD, catch_sigchld);
		first_time = 1;
	}
	pid = vfork();
	if (pid < CHILD) {
		/* error */
		(void) printf("Unable to fork\n");
		(void) exit(-1);
	} else if (pid > CHILD) {	/* parent process */
		(void) printf("\t\t\tI'm the PARENT. My child is [#%d]\n", pid);
	} else {
		/* child process */
		(void) execl(program, program);
	}
	return;
}

wait3_handler(lpid)
	int             lpid;
{
	int             catch_sigchld();

	(void) signal(SIGCHLD, catch_sigchld);

	/*
	 * do something with pid
	 */
	(void) printf("Do something with PID #%d\n", lpid);
	return (0);
}

catch_sigchld()
{
	int             lpid, status = 0;
	long            oldmask;

	while ((lpid = wait3(&status, WNOHANG, NULL)) > 0) {
		(void) wait3_handler(lpid);
		oldmask = sigblock(sigmask(SIGCHLD));
		(void) sigsetmask(oldmask);
	}
}