LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   perl threads sub-process not dying properly when program killed (http://www.linuxquestions.org/questions/programming-9/perl-threads-sub-process-not-dying-properly-when-program-killed-864340/)

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

mpapet 02-22-2011 05:21 PM

Quote:

Originally Posted by matthewg42 (Post 4267587)
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?
...

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

This is why threaded application programming is harder. You need better thread management. There are modules that simplify thread management.

http://search.cpan.org/~elizabeth/Th...ead/Running.pm


All times are GMT -5. The time now is 10:17 AM.