Killing threads in perl

J

joergwenzel

Hi,

who can help me, to kill the threads in this script.
I create lot of thread with a tar. If the tar canceled with errors, i
have to kill the rest of
the threads with tar. I dont now what is the best way. I tested with
kill Hup, but doesn't work.

code:
..
# fill my array with
.....
my $cmd =
"GZIP=$gzip; $tar -C $startdir -X $tapedir/exclude -czf
$tapedir/$dname"
. ".tar.gz . ";
push( @restore, "$tar -C (Please set path here!) -xzvf
$dname.tar.gz" );
push( @todo, $cmd ); # push Task on TODO
....
..
while ( $#todo >= 0 && $result == 0) {
last if($result > 0);
my $cmd = pop(@todo) ;
$sem->down(); # only n threads on the same time
if ( $result == 0) {
my $thread = threads->new(
\&Task,
$cmd, # create a new thread
);
$thread->detach();
}
....
..

#-------------------------------------------------------------------------
sub Task {
my (
$cmd, # Parameter
) = @_;
my $id = threads->self->tid;
$thread_run++;
my $result += system ($cmd)/256;;
if ($debug == 1) { print " Return: $result \n\n"; }
$sem->up();
#$thread_run--;
printf "Thread %02d: fertig.\n", $id;
kill ("HUP", -$$);
if ($result >0){print "\n\n Fehler !!!!!!!!\n\n";
kill ("HUP", -$$);

}

}
 
G

gf

Hi,

who can help me, to kill the threads in this script.
I create lot of thread with a tar. If the tar canceled with errors, i
have to kill the rest of
the threads with tar. I dont now what is the best way. I tested with
kill Hup, but doesn't work.
Off the top of my head I'd say that you want to use fork() instead of
threads if you want to be able to kill the tar processes.

When I used threads I had multiple LWP sessions running and each
thread looped until a queued list of URLs was processed, then they'd
exit automatically. Your tar processes don't have the intelligence to
know when to quit, which is why you're in that corner.

perldoc -f fork

Also read through Perlfaq 8
 
E

Eric Schwartz

A brief comment on a matter unrelated to your direct question:

while ( $#todo >= 0 && $result == 0) {
last if($result > 0);
my $cmd = pop(@todo) ;

....

}

This is a more Perl-ish way to do that loop:

foreach my $cmd (@todo) {
last if $result > 0;

# do stuff with $cmd as per usual.
}

Note that you don't have to check $result in the condition, since you
do so in the loop. You only need to check it one place, and doing it
in the loop makes the condition look cleaner. And using $#array in a
conditional looks kinda wonky, and is unnecessary most of the time
(I'd say "all of the time", but then somebody would come along and
find the .0001% case where it matters :).

-=Eric
 
Z

zentara

who can help me, to kill the threads in this script.
I create lot of thread with a tar. If the tar canceled with errors, i
have to kill the rest of
the threads with tar. I dont now what is the best way. I tested with
kill Hup, but doesn't work.

In order for a Perl thread to be killed individually, you need
to make it go to the end of it's code block, or do a return.

So, you need a shared variable in the thread
code, to tell it to return.

Like:

my $die:shared;
$die =0;

#in thread code check for it:
if($die){return}

If you don't detach, you still need to return from the thread
block, before a join can happen.

Joining is preferred over detaching, because you can reuse
the thread, or it's space, and that will avoid gaining memory as
you constantly spawn threads.

In c, threads work alot cleaner, but in Perl, you need to watch
for memory accumulating, and that means reusing threads.

zentara
 
J

joergwenzel

Like:

my $die:shared;
$die =0;

#in thread code check for it:
if($die){return}

If you don't detach, you still need to return from the thread
block, before a join can happen.

Joining is preferred over detaching, because you can reuse
the thread, or it's space, and that will avoid gaining memory as
you constantly spawn threads.

Yes, but my tar still running at the thread? My $return come back
at the end of the tar. the next line of the script start behind the
tar
no "if" help me at this time.

threads:

1. running tar 50min finished ok
2. running tar 30min breakup error
3. running tar 90min running <-- to kill
4. running tar 120min running <-- to kill
 
Z

zentara

Yes, but my tar still running at the thread? My $return come back
at the end of the tar. the next line of the script start behind the
tar
no "if" help me at this time.

threads:

1. running tar 50min finished ok
2. running tar 30min breakup error
3. running tar 90min running <-- to kill
4. running tar 120min running <-- to kill

Without seeing your thread code, you have 2 options.
1. Write the tar procedure in chunks, and in between chunks,
test for $die.

2. Find the pid of the tar command after it's launched in the thread,
and put it in a shared variable. Then have the main thread kill -9 it,
before you set $die = 1.


Just ideas,
zentara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top