Perl Threads Terminating Abnormally.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Threads Terminating Abnormally.
# 1  
Old 02-09-2013
Perl Threads Terminating Abnormally.

I've used threads before, but not with Perl.
I tried looking up these errors and using 'join' instead of 'detach' with no luck.
Here is the code I am currently using:

Code:
#!/usr/bin/perl -w

use warnings;
use threads;
use threads::shared;

$Linux='Linux';
$Greek='Greek';

      my $thr1 = threads->create(\&work1);

      sub work1 {
            open(FILE,"/home/mine/text");
              if (grep{/$Greek/} <FILE>){
                print "/home/mine/text exists!\n"
              }else{
                system("echo '' > /dev/null");
              }
          close FILE;
       $thr1->detach();
     }

     my $thr2 = threads->create(\&work2);

     sub work2 {
           $filename = "/home/mine/kcron";
             if (-e $filename) {
               print "/home/mine/kcron exists!\n";
             }else{
               system("echo '' > /dev/null");
             }
         $thr2->detach();
}
     my $thr3 = threads->create(\&work3);

     sub work3 {
           open(FILE,"/home/mine/geek.txt");
             if (grep{/$Linux/} <FILE>){
               print "/home/mine/geek.txt exists!\n";
             }else{
               system("echo '' > /dev/null");
             }
          close FILE;           
       $thr3->detach();
}

select(undef,undef,undef,.1);
exit();

These are the current errors I am getting:

Code:
# ./unixthrd.pl
/home/mine/text exists!
Thread 1 terminated abnormally: Can't call method "detach" on an undefined value at ./unixthrd.pl line 21.
/home/mine/kcron exists!
Thread 2 terminated abnormally: Can't call method "detach" on an undefined value at ./unixthrd.pl line 33.
/home/mine/geek.txt exists!
Thread 3 terminated abnormally: Can't call method "detach" on an undefined value at ./unixthrd.pl line 45.
Perl exited with active threads:
    0 running and unjoined
    3 finished and unjoined
    0 running and detached

Any advice much appreciated.

Last edited by Azrael; 02-09-2013 at 06:57 AM..
# 2  
Old 02-11-2013
Threads die naturally when the subroutine is complete (return). Detach is for different situations, I believe.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 02-12-2013
Finally got it!
Code:
#!/usr/bin/perl -w

use threads;
use threads::shared;

$Linux='Linux';
$Greek='Greek';

      sub work1 {
            open(FILE,"/home/ph33r/text");
              if (grep{/$Greek/} <FILE>){
                print "/home/ph33r/text exists!\n";
              }else{
                system("echo '' > /dev/null");
              }
          close FILE;
     }
     my $thr1 = threads->create(\&work1);
     $thr1->join();

     sub work2 {
           $filename = "/home/ph33r/kcron";
             if (-e $filename) {
               print "/home/ph33r/kcron exists!\n";
             }else{
               system("echo '' > /dev/null");
             }
     }
     my $thr2 = threads->create(\&work2);
     $thr2->join();


     sub work3 {
           open(FILE,"/home/ph33r/geek.txt");
             if (grep{/$Linux/} <FILE>){
               print "/home/ph33r/geek.txt exists!\n";
             }else{
               system("echo '' > /dev/null");
             }
           close FILE;
     }
     my $thr3 = threads->create(\&work3);
     $thr3->join();

With all threads joined the unwanted error finally goes away:
Code:
# ./unixthrd.pl 
/home/ph33r/text exists!
/home/ph33r/kcron exists!
/home/ph33r/geek.txt exists!

Much thanks to DGPickett for the motivation!
# 4  
Old 02-12-2013
While there are locks like mutex, I like to use threads in a flow paradign like shell scripts, with pipes or ring buffers from one to the next. On thread can be a dispatcher, writing buffers to many threads, and another might be a collector, reading buffers from many threads. Pipes let your threads block, which is nice as threads should not consume CPU when idle, but who wants to sleep and poll (sleep short and waste CPU, sleep long and add latency)? You can poll() or select() if you have many pipes to block on. Dispatcher and collector could be the same thread, so it can add or remove threads for demand.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using threads in perl

Hi everyone, I am trying to create a script which runs a number of processes simultaneously and at the same time use a timer to keep track of what is going on. The problem is that the timer stops and the script exits upon the completion of some of the processes, whereas I want to timer to... (0 Replies)
Discussion started by: free2rhyme2k
0 Replies

2. HP-UX

logrotate: ALERT exitted abnormally with [127]

HP-UX B.11.23 U 9000/800 636114222 unlimited-user license I see this error in my logs sometimes and have very little info on it... I have searched online for some documentation and haven't been able to find much on this issue. The only thing I found on one site is that if my /tmp folder... (4 Replies)
Discussion started by: zixzix01
4 Replies

3. Shell Programming and Scripting

perl launch threads in an array variable?

Im having a problem launching multiple sub routines as threads. My script seems to stop when the first thread is launched. Im condensing the code for simplification here: #!/usr/bin/perl -w use strict; use threads; srand; my ($cnt,$line,$iprange_rand); my... (2 Replies)
Discussion started by: trey85stang
2 Replies

4. Shell Programming and Scripting

How can i terminating expect script without terminating SSH connection.

Hi all , i know i ask a lot of question but these are really hard to solve and important question. I send two scripts: expect.sh: #!/usr/local/bin/expect spawn ssh root@172.30.64.163 expect "login:" send "root\n" expect "password:" send "root\n^M" interact and son.sh: ... (2 Replies)
Discussion started by: fozay
2 Replies

5. Shell Programming and Scripting

Perl v5.8.5 Threads Problem

Hi Unix gurus, I am facing a threading problem in Perl. I have a worker thread in perl in which I am calling a shell script. The shell script echo's output to the Standard Output from time to time as it progresses. In the worker thread, I am unable to display the echo statement of shell... (1 Reply)
Discussion started by: som.nitk
1 Replies

6. Shell Programming and Scripting

mpack behaves abnormally

Hi, I was using mpack to send mails using cronjob with attachments. It was working perfect. But recently it's behaving strangely. Its sending the mails without any error message but the mail is not getting delivered. The code I was using: /usr/local/bin/mpack -s "$SUBJECT" -d $MSGBODY... (0 Replies)
Discussion started by: itesh.dash
0 Replies

7. UNIX for Advanced & Expert Users

Threads terminating when a signal is generated

hi all, i had created 3 threads using pthreads. Each thread does a different job. Now the main problem is when one thread generates a signal (for example SIGFPE, SIGINT) then the process terminates and all other threads do terminate eventually. I want to keep other threads running i.e, i... (0 Replies)
Discussion started by: skyrulz
0 Replies

8. UNIX for Advanced & Expert Users

command terminated abnormally

I am getting an Error Message as ============================= starting Sortcl Merging And Aggregation time: command terminated abnormally. ============================= , when running a sortcl command.sortcl is a command for sorting large data,using cosort application installed in... (0 Replies)
Discussion started by: tkbharani
0 Replies

9. Shell Programming and Scripting

new to perl.. how do I do this ? fork/ threads ?

I have never coded in perl before (just started today morning :). I need to write a perl program to automate a task. Here is how I do it manually: Start a program in my home dir. Now if I want to execute another program while this one is still running, what I would do is go to another... (6 Replies)
Discussion started by: the_learner
6 Replies

10. UNIX for Dummies Questions & Answers

Terminating child script with terminating the parent script

Hi I was working on a shell script with randomly shows a page of text from a randomly selected topic .As soon as the page is displayed it callers a timer script which keeps on running indefinitely until the timer script is killed by the user. This is where I have the problem,if I press... (2 Replies)
Discussion started by: mervin2006
2 Replies
Login or Register to Ask a Question