use strict; use warnings; our $children; our $new_kids; FORK: { if ( !$children ) { print 'How many children to start with? [5]: '; $children = <>; chomp $children; unless ($children) { $children = '5'; } die "only numbers for kids, please\n\n" if $children =~ /\D/; for ( 1..$children ) { my $child; my $signal = $_; die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { # i'm the child! &dummy(); } else { # i'm the parent while ( $signal ge $children ) { &interact(); redo FORK; } } } } else { for ( 0..$new_kids ) { my $child; my $signal = $_; die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { # i'm the child! &dummy(); } else { # i'm the parent while ( $signal ge $new_kids ) { &interact(); redo FORK; } } } } } sub dummy() { sleep 90; exit; } sub interact() { print 'Current number of children is: ', "$children", '. i.e. these:', "\n\n"; system('ps -ef | grep tryMore | grep -v grep'); print "\n\n"; print 'Minus one for the parent, of course', "\n\n"; print 'How many children do you want to add? [1]: '; my $new_kids = <>; chomp $new_kids; $new_kids = '0' unless $new_kids; unless($new_kids) { $children += 1; } else { $children += $new_kids; } return; }