我在我的 Perl 代码中发布了几个 关于数组和线程的问题。这里又是代码。根据建议,我进行了更改,它似乎正在工作。代码仍然遇到了新问题。机器内存不足。许多线程已完成且未连接。
代码-
#!/usr/bin/perl -w -I /opt/hypertable/0.9.7.3/lib/perl -I /opt/hypertable/0.9.7.3/lib/perl/gen-perl
use strict;
use IO::Socket;
use Geo::IP;
use threads qw(stringify);
use Net::NBName;
use Data::Dumper;
use Hypertable::ThriftClient;
# Syslog Variables and Constants
my $MAXLEN = 1524;
my $limit = 5; #for testing
my $sock;
my $thr;
# Start Listening on UDP port 514
$sock = IO::Socket::INET->new(LocalPort => '514', Proto => 'udp') || die("Socket: $@");
my $buf = '';
do{
my $count = 0;
my @set;
for ($count = 0; $count <= $limit; $count++) {
$sock->recv($buf, $MAXLEN);
my ($port, $ipaddr) = sockaddr_in($sock->peername);
my $hn = gethostbyaddr($ipaddr, AF_INET);
$buf =~ /<(\d+)>(.*?):(.*)/;
my $msg = $3;
$set[$count][0] = $hn;
$set[$count][3] = $msg;
print $count." --> ".$set[$count][0]." --> ".$set[$count][4]."\n";#Print original array, should print 5 elements
$thr = threads->create('logsys',@set);
#&logsys(@set);
}
}while(1);
$thr->join();
sub logsys {
my $count = 0;
my @set = @_;
my $geoip = Geo::IP->new(GEOIP_CHECK_CACHE);
my $nb = Net::NBName->new;
print "--------------------- ".scalar (@set)." -------------------\n";
for ($count=0; $count <= $limit; $count++) {
print $count." --> ".$set[$count][0]." --> ".$set[$count][5]."\n";#print passed array, should same exact 5 elements
if (open(WW,">syslog")){
print WW $count." --> ".$set[$count][0]." --> ".$set[$count][6]."\n"; close(WW);
}
}
}
错误 -
Out of memory!
Callback called exit at /usr/lib/perl/5.10/IO/Socket.pm line 287.
Thread 646 terminated abnormally: main=HASH(0xee3b3068) at ./syslogd.pl line 50.
Perl exited with active threads:
9 running and unjoined
644 finished and unjoined
0 running and detached
这是什么意思?为什么即使完成了许多线程,机器也会耗尽内存?我该如何解决?如果需要,我可以提供更多信息。