[comp.unix.xenix] help determining child process status

msb62@leah.Albany.Edu (Mitch Baltuch) (06/06/89)

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?

Thanks
Mitch Baltuch
Thunderstorm Analysis Center
State University of New York at Albany
____________________________________________________________________________
Internet: msb62@leah.albany.edu		Phone: (518) 442-4138
Bitnet:   m.s.baltuch@albnyvms		Snail Mail: State Univ. of NY at Albany
						    ES235	ATM/SCI
						    1400 Washington Ave.
						    Albany, New York 12222

Disclaimer: These opinions are only mine, but I love 'em anyway.

jbayer@ispi.UUCP (Jonathan Bayer) (06/07/89)

In article <1854@leah.Albany.Edu> msb62@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@ispi.UUCP

kory@avatar.UUCP (Kory Hamzeh) (06/09/89)

In article <1854@leah.Albany.Edu>, msb62@leah.Albany.Edu (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?

If you know the pids of the sub-tasks, you can do a kill(pid, 0). Under
Xenix, kill(pid, 0) returns 0 (zero) if the task is still running or 
-1 if the task is terminated. I have tried this under Xenix and it works.
However, I know that it is not very portable to other flavors of Unix.

Hope this helps.

--kory



-- 
-------------------------------------------------------------------------------
Kory Hamzeh			    UUCP:     ..!uunet!psivax!quad1!avatar!kory
				    INTERNET: avatar!kory@quad.com

guy@auspex.auspex.com (Guy Harris) (06/29/89)

 >If you know the pids of the sub-tasks, you can do a kill(pid, 0). Under
 >Xenix, kill(pid, 0) returns 0 (zero) if the task is still running or 
 >-1 if the task is terminated. I have tried this under Xenix and it works.
 >However, I know that it is not very portable to other flavors of Unix.

Uhh, it works under 4.[23]BSD and systems derived therefrom, and System
V Release N for values of N greater than some M (M is at least 2, as I
remember) and systems derived therefrom, so it's more portable than you
might think.