SPUG:SUMMARY: Child Processes

North, Walter wnorth at state.mt.us
Tue Feb 18 12:09:54 CST 2003


Thanks for the responses from:

Peter Darley
David Dyck
Brian Hatch
Jay Scherrer

(Original post included below)

Brian and David brought up the simple method I was looking for

while ( wait() != -1 ) {
	# do nothing
}

At this point I was still lost.
> 	I feel brain dean here.  Obviously I am missing something
> 	important. No doubt it is one of those things where you bang
> 	your head on the keyboard after finding out what it is.
>

But as Brian pointed out,
I was creating GRANDCHILDREN not children

And yes it was a head banging moment

BANG, BANG, BANG...............

The script Brian sent works just fine, except for the missing `}'
for the do statement.

(Brians script after the original post)

Again thanks for the help.

------------original post--------------------------
I'm sure there is a simple answer for this but
for some reason, not excluding plain old stupidity, 
it seems to elude me.

I have a script that releases a varying number of other scripts.

I am trying to make the parent wait until all the children finish,
 but other then the brute force method of looping thru watching the 
processes in the process list while looking for a key phrase I have 
not been able to make the parent wait for all the children to finish.

It seems to me there must a better way of doing this.

In Korn shell you can put in a wait statement after releasing the
children and all is well, but perl does not seem to have a simple
equivilent.

I've tried assorted methods with no success so far.  The Perl
books leave me baffled on this.

Would some kind person please give me a hint or some such.

Thanks in Advance.
-----------------------------------------------------------------

----------------------Brians script------------------------------
  until ($CHILDREN == $X) {
	print "Number of children is $CHILDREN\n";

	$T = ($CHILDREN + 10);
	print "Child $CHILDREN is sleeping for $T seconds\n";

	my $child_pid = fork();
	if ( not defined $child_pid ) {
		die "We can't fork - out of processes?"
	}
	if ( not $child_pid) {
		# we're a child process


		exec $PROG, $T;

		# We could use 'system "$PROG, $T"' but
		# I prefer avoiding system whenever possible
		# since it spawns a shell unnecessarily, and
		# if you're using user input the shell can be
		# abused.  Better to learn to use exec properly.
		#
		# Alternatively, you could use "system $PROG, $T"
		# which does not spawn a shell (the command is
		# supplied as separate arguments to system, not
		# as a string.)

		# If the exec works, no need for exit, but let's
		# be safe - we don't want our child acting like
		# our parent if the exec fails, or if we change
		# exec to system, which *will* continue processing.

		exit 0;
	}
	# if parent, just keep going

	$CHILDREN++;
  }

  # Changed to a do/while loop so the print happens before we wait.
  do {
	print "Waiting on the children to finish\n";

	#sleep 5;
	# No need to wait usually - wait will block until a child
	# is ready.

  while ( wait() != -1 );

  print "All kids done.\n";
----------------------------------------------------------------



More information about the spug-list mailing list