Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using alarm to set a timeout

Status
Not open for further replies.

droid99

Programmer
Jul 6, 2004
5
US
I am using the following code to read in a web site access log file and translate IP addresses to their domain names:

1 open(LOG, "$logfile");
2 open(TEMP, ">$tempfile");
3 while(my $line = <LOG>)
4 {
5 if($line =~ /^(\d{2,}\.\d{2,}\.\d{2,}\.\d{2,})/)
6 {
7 my $ip_address = $1;
8 unless (exists $hostname{$ip_address})
9 {
10 $hostname{$ip_address} = gethostbyaddr
(inet_aton($ip_address), AF_INET);
11 print TEMP "$ip_address -> $hostname{$ip_address}\n";
12 }
13 }
14 }
15 close TEMP;
16 close LOG;


Here is my question: Line 10 can take a long time to process just one IP address to domain name lookup. How can I use alarm to set a timeout after 1 second has passed?

Thank you very much for any help.
 
Code:
1  open(LOG, "$logfile");
2  open(TEMP, ">$tempfile");
3  while(my $line = <LOG>)
4  {
5      if($line =~ /^(\d{2,}\.\d{2,}\.\d{2,}\.\d{2,})/)
6      {
7          my $ip_address = $1;
8          unless (exists $hostname{$ip_address})
9          {
10             eval {
11               local $SIG{ALRM} = sub { die "alarm clock restart" };
12               alarm 10;
13               $hostname{$ip_address} = gethostbyaddr
                     (inet_aton($ip_address), AF_INET);
14               alarm 0;
15             };
16             if ($@ and $@ !~ /alarm clock restart/) { die }
17             print TEMP "$ip_address -> $hostname{$ip_address}\n";
18         }
19     }
20 }
21 close TEMP;
22 close LOG;
You can obviously change the "die" on line 16 to do whatever you want it to do on timeout.

Trojan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top