Unfortunatly this could produce zombies. How could I prevent this?
A fairly standard perl "reaper" construct is:
use POSIX qw/WNOHANG/; ## ... $SIG{ CHLD } = sub { while ( waitpid( -1, WNOHANG ) > 0 ) { ## what to do, empty here } }; ## fork down here
Look up wait() and waitpid(), those are the functions to help you with terminating children.
The following is a little touched up verbose version you can build from.
#!/usr/bin/perl -w use strict; use POSIX qw/WNOHANG/; my $time = 5; $SIG{ CHLD } = sub { while ( ( my $x = waitpid( -1, WNOHANG ) ) > 0 ) { print "\tprocess '$x' got reaped.\n" } }; my @toDoList = ( 'ls -la', 'echo Yay!', 'date', 'perl -V', ); foreach my $doIt ( @toDoList ) { my $child = fork(); defined $child or die "Can not fork: $!"; if ( $child > 0 ) { ## parent print "Parent of $child sleeping for $time seconds . . .\n"; sleep( $time ); print "Parent of $child done sleeping!\n"; } else { ## child print "Child, going to do '$doIt'\n"; exec( "$doIt && echo '** Finished \"$doIt\" **'" ); } } print "\nDone.\n\n"; exit(0);
I added && echo '** Finished \"$doIt\" **' to the exec call just to be even more verbose, it is not needed, nor wanted, in your actual code
Will perl for money
JJ Knitis
(901) 756-7693
gt8073a@industrialmusic.com
In reply to Re: zombies after forking
by gt8073a
in thread zombies after forking
by very empty
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |