The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Sending sig INT (control C) to threads
Discuss Sending sig INT (control C) to threads in the Perl Programming forum on Dev Shed. Sending sig INT (control C) to threads Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 28th, 2006, 04:09 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Posts: 44
Time spent in forums: 6 h 20 m 45 sec
Reputation Power: 10
|
|
|
Sending sig INT (control C) to threads
Hello,
I've seen the documentation of threads with perl and tryed to do what they said to interrupt a thread after Control-C is pressed.
http://search.cpan.org/~jdhedden/threads-1.57/threads.pm#THREAD_SIGNALLING
Unfortunately it makes an error
Code:
#!/usr/bin/perl -w
use strict;
use threads;
sub f1
{
$SIG{'INT'} = sub { print "Thread exit\n"; threads->exit(); };
while(1)
{
print "SLEEP\n";
sleep(2);
}
}
my $thr = threads->new('f1');
$thr->kill('INT')->detach();
Code:
$ ./sigint.pl
Can't locate auto/threads/kill.al in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./new.pl line 19
A thread exited while 2 threads were running.
Strange because I created only one thread not two.
|

September 7th, 2007, 01:32 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 104
Time spent in forums: 1 Day 5 h 47 m 55 sec
Reputation Power: 10
|
|
|
Ever find a solution for this? I'm stuck on the same thing...
Oh btw, if you spawn a thread, you're going to have two threads: your new thread, and the main thread of execution.
|

September 7th, 2007, 03:52 PM
|
|
|
Quote: | Originally Posted by BenZarboni Ever find a solution for this? I'm stuck on the same thing...
Oh btw, if you spawn a thread, you're going to have two threads: your new thread, and the main thread of execution. |
ActiveState 5.8.8 on Win2k
Code:
#!/usr/bin/perl -w
use threads;
use strict;
use lib 'library';
use LOG;
use Smart::Comments;
sub f1 {
$SIG{INT} = sub {
### Thread exit! ...
threads->exit();
};
while(1) {
### SLEEPING ...
sleep(2);
};
} # f1: well, Monza is SUNDAY
### Starting a thread, dude ...
# AND that "my $thr = threads->new('f1');" is just wrong
my $thr = threads->new(\&f1);
### Wait until the bloody thread has started before you try sending a signal ...
sleep(1);
### Send a signal to the thread ...
$thr->kill('INT');
### Collect the bits and pieces! ...
$_->join foreach threads->list;
__END__
produces
Code:
duh.pl(996) 0 Fri Sep 7 15:49:28 2007 duh.pl(0) << duh.pl >>
duh.pl(996) 0 Fri Sep 7 15:49:28 2007 duh.pl(20) << ### Starting a thread, dude ... >>
duh.pl(996) 0 Fri Sep 7 15:49:28 2007 duh.pl(23) << ### Wait until the bloody thread has started before you try sending a signal ... >>
duh.pl(996) 1 Fri Sep 7 15:49:28 2007 duh.pl(15) << ### SLEEPING ... >>
duh.pl(996) 0 Fri Sep 7 15:49:29 2007 duh.pl(25) << ### Send a signal to the thread ... >>
duh.pl(996) 0 Fri Sep 7 15:49:29 2007 duh.pl(28) << ### Collect the bits and pieces! ... >>
duh.pl(996) 1 Fri Sep 7 15:49:30 2007 duh.pl(11) << ### Thread exit! ... >>
Yes, the number between the (996) and Fri Sep is the thread id!
__________________
"Plagiarize the code of others, I say!"
That's about the worst you can do. It's the fast way to make a very bad coder out of you. It's important to understand what you are doing. Just asking what you should do, and blindly copying that if you would have gotten an unambigious answer is very, very bad.
-- Abigail
|

September 10th, 2007, 08:21 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 104
Time spent in forums: 1 Day 5 h 47 m 55 sec
Reputation Power: 10
|
|
Hey;
Thanks for the sample code. Helped me clean some of my stuff up..
The problem for me however lies elsewhere.
In your code or mine, whenever I try to CTRL-C and catch that signal + terminate my threads, I get two problems:
1) The same as the original poster:
$thr->kill('INT'); produces
Quote:
$ ./sigint.pl
Can't locate auto/threads/kill.al in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at ./new.pl line 19
A thread exited while 2 threads were running.
|
2) My threads block until they've completed whatever they're doing.. So CTRL-C is blocked until the thread is finished, which prevents me from doing what I want (killing the program immediately)...
hmm..
|

September 10th, 2007, 08:43 AM
|
|
|
|
Post code --- Note that you need to insure that the thread is running and has the SIG handler installed before you attempt the signal ... which is why the sleep 1 in my example.
|

March 2nd, 2012, 05:35 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 1
Time spent in forums: 1 m 56 sec
Reputation Power: 0
|
|
|
Рецепты народ
Здравствуйте! Подскажите пожалуйста, кто сталкивался с: - лечение лекарственными травами - народный целитель Кто пользуется народными средствами?  P.s. Перенесите в нужный раздел, если не туда попал  Спасибо!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|