help determining child process status

Jonathan Bayer jbayer at ispi.UUCP
Thu Jun 8 00:18:14 AEST 1989


In article <1854 at leah.Albany.Edu> msb62 at leah.albany.edu.UUCP (Mitch Baltuch) writes:
>I am running SCO Xenix v2.3 on a Zenith Z386, using their v2.3 development
>toolkit.  My problem is that I have a parent task which forks a number
>of tasks.  Some of these tasks spawn other tasks based on asynchronous
>events, so that the number and duration of these tasks cannot be determined.
>I am looking for a way that a task can tell when all its children have
>terminated.  This is complicated by the fact that one or two processes may
>not terminate by themselves and cannot be terminated until all other
>processes have exited. I have not been able to find a convenient way to
>accomplish this.  Any suggestions?


Yes.  Read the man page for wait().  All you have to do is to set up a
list of all the child processes that you have to wait for them to
terminate, wait for each one, and then kill the rest.  Some sample code
follows.  Please note that I did not write in any error-checking code. 
I leave that as an exercise for the reader.

JB


main()
{
int	wait_list[100], kill_list[10];
int	num_to_wait = 0,	num_to_kill = 0;
int	x;

	/* spawn child processes here using fork, fill in appropriate list */

	if ( (x = fork()) != 0) {
		wait_list[num_to_wait++] = x;
	}

	if ( (x = fork()) != 0) {
		kill_list[num_to_kill++] = x;
	}


	/* wait for those processes to terminate on their own */
	
	while (num_to_wait--)
		wait( wait_list[num_to_wait] );
		

	/* kill those you want to kill */

	while (num_to_kill--)
		kill( kill_list[num_to_kill], 9 );

}

-- 
Jonathan Bayer			      Beware: The light at the end of the
Intelligent Software Products, Inc.	      tunnel may be an oncoming dragon
500 Oakwood Ave.				...uunet!ispi!root
Roselle Park, NJ   07204    (201) 245-5922    jbayer at ispi.UUCP



More information about the Comp.unix.questions mailing list