So I am trying to create a Perl program that forks a worker and waits for it to finish. In my real use case I need to fork many workers and wait for them all, so I thought I would try a test case with a single worker. My concern here is that when I run this program in the terminal, sending a ^C doesn't kill the parent, even though the signal handler seems like it should reap the child process and cause the parent to exit normally. I am trying to use waitpid to keep the parent alive so that it can receive signals and pass them along to the child, but the parent process seems to ignore ^C entirely.
use strict;
use warnings FATAL => 'all';
use POSIX ":sys_wait_h";
my $cpid;
sub reap_children_and_exit {
defined $cpid and kill 'HUP', $cpid;
exit 0;
}
$SIG{INT} = \&reap_children_and_exit;
my $child_text = <<'EOF';
perl -e 'while (1) { printf "%s\n", rand() }'
EOF
$cpid = fork;
if ($cpid == 0) {
exec($child_text)
}
waitpid($cpid, WNOHANG);