matthewg42 |
02-22-2011 12:36 PM |
perl threads sub-process not dying properly when program killed
I have a perl threaded program, which spawns tail in one of the threads.
When if the program is killed with a signal (e.g. killall myprogram), the main program dies, but the child tail process gets orphaned and doesn't die. How can I make it die when the parent does?
Simplified code which exhibits the same problem:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
my $h = threads->create('heartbeat');
my $t = threads->create('tailer');
# This doesn't seem to make a difference
$t->detach();
# This is how I determine if my process is done...
$h->join();
exit();
sub tailer {
open(TAIL, "tail -f t.pl|") || die "$!";
while(<TAIL>) {
print "tailer: $_";
}
return 1;
}
sub heartbeat {
while(1) {
sleep 5;
print "<3\n";
}
}
I tried a signal handler in the thread, but it doesn't seem to get called. My actual program is a LOT more complicated (daemonized, more threads etc.) but I think if I can get this example working, I can solve it in my actual code...
Cheers
|