UNIX Socket FAQ

You are not logged in.

  • Index
  • » Threads
  • » Perl with threads and alarm() is possible?

#1 2010-05-10 04:51 PM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

Hi gurus,

I'm a very new perl programmer and I'm writing my first perl code, however I'm having a few problems. The first was related with my actual problem, I have a code that connects to different ports and check for SOCKS, however it took very long to return if the server was not a SOCKS server, also I need to use threads to make it less slow and scan multiple services at the same time.

The suggestion was to use alarm() and it worked fine.

http://www.perlmonks.org/index.pl?node_id=838963

Well, it worked fine in a single test script without threads, when I start to use threads on my real code all get messed up.

I get all the time a error "Alarm clock" and the process die / killed, however I never defined a entry with this output "Alarm clock".

My code is like that:

#!/usr/bin/perl
use Socket;
use IO::Socket;
use Net::SOCKS;
use threads;
use threads::shared;

my ($host, $port) = @ARGV;

mfunc();

$port = 1080;
mfunc();

$port = 80;
mfunc();

for my $lthrd (1..7){

my $nthrd = threads->new(\&mfunc);
	foreach (threads->list){

		$_->join;
	}

}

$port = 22;
mfunc();

sub mfunc{

eval {
local $SIG{ALRM} = sub { die "alarm\n" };       # NB \n required
alarm 7;

my $sock = new Net::SOCKS(socks_addr => $host, socks_port => $port, protocol_version => 4);

my $rsocks = $sock->connect(peer_addr => 'www.google.com', peer_port => 80);

my $status = $sock->param('status_num');

if ($status == SOCKS_OKAY){

print "\nWorked $host:$port\n";
$sock->close();
}

alarm 0;
};

die if $@ && $@ ne "alarm\n";       # propagate errors

if ($@) {

print("\nTimeout $host:$port\n");

 }

}

If you try you will get

./test.pl www.google.com 1080

Test 01

Timeout www.google.com:1080

Timeout www.google.com:3128
Alarm clock

It proves that the code works fine out of the thread, but on the threads it get completely crazy.

I saw this possible suggestion to fix the problem:

http://www.mail-archive.com/perl5-porte … 89360.html

However, now the code gives this error and back to be very slow again, probable when the first alarm() happen it get unhanded and never more work.

SIGALRM handler "a" not defined.

I can't believe there is no way to fix it in perl, can someone please help me in fix this code (examples are very welcome) to work with the threads and with the alarm()? Or any other approach that make the code fast and not waiting 5 minutes per connection that is not SOCKS...

Well, as I know you are gurus in C my other question is if this problem apply to C or it's a exclusive perl problem?

Thank you

Offline

#2 2010-05-10 07:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,736
Website

Re: Perl with threads and alarm() is possible?

Mixing signals (which is what alarm() uses) with threads is always a bad idea...
Depending on the implementation, you MAY be able to make things work, but it's still
ugly...  In C, I'd tell you to just use a non-blocking connect(), with select()/poll() or
similar function to do your timeouts in a thread-safe way...  I suspect perl has similar
calls, too...  This old FAQ entry shows how to do it in C...

Offline

#3 2010-05-10 11:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,187

Re: Perl with threads and alarm() is possible?

Or switch to multiple processes instead of threads.

But yeah, if you're going to open many connections anyway I'd switch to
non-blocking sockets with multiplexing too.

Offline

#4 2010-05-11 12:08 AM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

Hi RobSeace and i3839

The bad news is that appear that select() doesn't work with Net::SOCKS in perl. Too bad!

How could make this process with multiple process instead threads? I mean, because in threads we share data, in multiple process (fork) I will have different instances repeating the same task, which for sure will not make my program posted on the thread to be fast as it's with threads.

Thank you

Offline

#5 2010-05-11 01:22 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,187

Re: Perl with threads and alarm() is possible?

Umm, there is zero data sharing after thread creation in the code you posted.
Doing the same, but with fork should be hardly slower. You can still kill all
other childs if one of them gets a connection and you don't want the rest to do
anything.

There must be way to fish out the underlaying file descriptor/socket from
Net::SOCKS, so if you have a select or poll in perl it should work.

Offline

#6 2010-05-12 05:16 PM

Rick
Member
Registered: 2009-10-21
Posts: 8

Re: Perl with threads and alarm() is possible?

Hi i3839

Thanks for the answer, I'm researching.

Offline

  • Index
  • » Threads
  • » Perl with threads and alarm() is possible?

Board footer

Powered by FluxBB