#!/usr/bin/perl use strict; use POSIX qw(:signal_h :errno_h :sys_wait_h); $SIG{CHLD} = \&REAPER; my (@all_kids, @dead_kids); my @array = qw(a b c d e f g h); for (1 .. 10) { my $pid = fork(); if ($pid) { push (@all_kids, $pid); } elsif ($pid == 0) { print "@array\n\n"; sleep 5; exit (0); } else { die "Could not fork: $!\n"; } print "Running another child process - $pid.\n"; } print "All child processes have been ran.\n"; print "@all_kids\n"; $SIG{INT} = \&tsktsk; foreach (@all_kids) { waitpid ($_, 0); } print "All child processes are finished.\n"; sub tsktsk { print "Ctrl-C Trap\n"; my %alive_kids; for my $i (@all_kids, @dead_kids) { $alive_kids{$i}++; } for (sort keys %alive_kids) { print "$_\n" if ( $alive_kids{$_} == 1 ); } exit; } sub REAPER { my $pid; $pid = waitpid(-1, &WNOHANG); if ($pid == -1) { } elsif ( WIFEXITED($?) ) { print "Process $pid exited.\n"; push( @dead_kids, $pid ) } else { print "False alarm on $pid.\n"; } $SIG{CHLD} = \&REAPER; }