LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-22-2011, 12:36 PM   #1
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 02-22-2011, 05:21 PM   #2
mpapet
Member
 
Registered: Nov 2003
Location: Los Angeles
Distribution: debian
Posts: 453

Rep: Reputation: 46
Quote:
Originally Posted by matthewg42 View Post
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

Last edited by mpapet; 02-22-2011 at 05:32 PM.
 
  


Reply

Tags
kill, perl, process


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why exactly was my process killed ??? qwerty47 Linux - Newbie 9 12-12-2013 12:08 AM
find history of a job killed by "kernel: Out of Memory: Killed process" poulacou Linux - Server 3 09-20-2007 04:24 PM
Is Perl a dying language? Micro420 Programming 37 05-31-2007 12:49 PM
my get new threads from the forums perl program dmx9595 Programming 0 09-06-2003 11:28 AM
process is getting killed automatically chens_83 Linux - General 2 09-16-2002 03:52 AM


All times are GMT -5. The time now is 07:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration