0
votes

I have a perl program, that spawns several threads. Each thread processees some task (by firing off other system commands etc) and then when its all done, Waits.

Once all threads are done, they fire a signal to Parent process. The parent then loads up new jobs, and signals the threads to go work on these new tasks.

So ideally, this program, would run forever.

Now, if I kill it in command line with kill -9 MainProgram.pl, its not killed! I see the output of the jobs the threads are running, and then I also see that after they are done, they getnew jobs and just go on and on...

I am absolutely confounded. If I do a kill -9 MainProgram.pl, it is supposed to kill all threads it owns, right? Regardless of what the threads are out doing....

And even if the threads are doing I/O and so they wait for the IO to get done...I would expect the thread to die after its current task is done..but clearly, Main is reloading jobs too, as threads just keep continuing...

Is this kind of behaviour seen in perl ?

EDIT: Some of the code in mainProgram.pl

use threads;
use threads::shared;

for (my $count = 0; $count <= $threadNum-1; $count++) {
  $t = threads->new(\&handleEvent, $count) ;
  push(@threads, $t);
}

#Parent thread:
while(1) {
  lock($parentSignal);
  cond_wait($parentSignal);
  getEvents();
  while(@eventCount== 0){
    sleep($parent_sleep_time);
    getEvents(); #Try to get events again until you get some new stuff to process
  }
  cond_broadcast($threadsDone); # threadsgo work on this
}

Thanks

1
Which threads? Perl has at least two implementations of threading.el.pescado
It would be really good if you could post a runnable example, so that we can verify the problem. Without such code, only questions remain: Where does getEvents come from? Why are you using shared variables instead of semaphores for communication? What are the contents of handle_Event?amon
What happens if you try killing the PID, rather than the filename, of the process? kill -9 can't be avoided, after all. I'd say you're doing it wrong.pilcrow
kill -9 MainProgram.pl is not proper usage for kill.ikegami
@el.pescado, No, it just has one. (Well, there's also Coro, but that's clearly not being used.)ikegami

1 Answers

0
votes

From what I understand, you're supposed to either join() or detach() on all threads prior to exiting.

From the POD:

If the program exits without all threads having either been joined or detached, then a warning will be issued.

Source: http://metacpan.org/pod/threads