#!/usr/bin/perl 
use Proc::Killfam;

#Alarm Signal Handler - kills all children subprocesses
$SIG{ALRM} = sub {
	print "$$: woke up after $timeout sec. Killing child family: $childpid\n";  
	# This kills all children processes 
	killfam 'KILL', $childpid; 
	# However, on Perl/Cygwin, the backticked `sleep` process is not killed
}; 

# Main routine - sets alarm and forks child to start a blocking `sleep`
sub timeout_test {
	print "Starting... my own PID=$$\n";
	$timeout =3;
	$childpid = fork();
	if ($childpid > 0) { # this is the parent process - wait for children to die
		print "$$: Forked child $childpid. Set alarm for $timeout seconds\n";
		alarm $timeout;
		do {
			$kid = waitpid (-1, 0);
		} until $kid == -1;
		exit;
	}
	$s = `/bin/sleep 7`;
	print "Sleep ended";
}

# Kick it all off 
timeout_test();



