免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2543 | 回复: 9
打印 上一主题 下一主题

求助:Perl多线程:全局变量无法push进值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-28 14:19 |只看该作者 |倒序浏览
#!/bin/perl
use IO::Socket;
use threads;
use strict;

my $ipHead="192.168.4.";
our @iplist=();
my @ips;
for(my $i=1;$i<255;$i++){push(@ips,$ipHead."$i");}

my $thread;
my @threads;
print localtime."\n";
for(my $j=0;$j<@ips;$j++){$threads[$j]=threads->new(\&portScaner,$ips[$j]);}
foreach my $threadinstance(@threads){$threadinstance->join();}
#foreach my $ip(@ips){&portScaner($ip);}
print localtime."\n";
print "@iplist\n";

#---------------------------------------------------------
#scan if the port 22 is open
sub portScaner{
my ($ip)=(shift);
my $result=IO::Socket::INET->new("$ip:22");
if($result){
print "$ip\n";
push(@iplist,$ip);
$result->close;
}
#else {writeLog("ip=$ip port 22 is not open!\n");}
}

这个程序如果用for循环执行@iplist数组中值是正确的,但是加上多线程后@iplist值输出时空的,求解:加lock(@iplist);也不好使。

论坛徽章:
0
2 [报告]
发表于 2013-04-28 14:55 |只看该作者
  1. use threads::shared;

  2. our @iplist :shared=();
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-04-28 15:02 |只看该作者
回复 2# iamlimeng
为什么我加lock也不好使呢,求讲解


   

论坛徽章:
30
水瓶座
日期:2014-08-22 21:06:3415-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:4715-16赛季CBA联赛之天津
日期:2019-01-11 01:11:44
4 [报告]
发表于 2013-04-28 15:16 |只看该作者
本帖最后由 shijiang1130 于 2013-04-28 15:16 编辑


shared搞起来
  1. #!/bin/perl
  2. use IO::Socket;
  3. use threads;
  4. use threads::shared;
  5. use strict;
  6. my $ipHead="127.0.0.";
  7. my (@ips,@ipList) :shared;
  8. my @threads;

  9. for(1..255) {
  10.         push(@ips,$ipHead."$_");
  11.         }

  12. print localtime."\n";

  13. for(my $j=0;$j<@ips;$j++) {
  14.         $threads[$j]=threads->new(\&portScaner,$ips[$j]);        }

  15. foreach my $threadinstance (@threads) {
  16.         $threadinstance->join();
  17.         }

  18. #foreach my $ip(@ips){&portScaner($ip);}

  19. print localtime."\n";

  20. #scan if the port 22 is open
  21. sub portScaner{
  22.                 my $ip = shift;
  23.                 my $result = IO::Socket::INET->new("$ip:22");
  24.                 if($result){
  25.                                 print "$ip\n";
  26.                                 push(@ipList,$ip);
  27.                                 $result->close;
  28.                 }
  29. #else {writeLog("ip=$ip port 22 is not open!\n");}
  30. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2013-04-28 15:25 |只看该作者
回复 4# shijiang1130
谢谢!可否给小弟讲解下shared和lock的区别。

论坛徽章:
0
6 [报告]
发表于 2013-04-28 20:45 |只看该作者
#!/bin/perl
use IO::Socket;
use threads;
use threads::shared;
use DBI();
use strict;

#---------------------------------------------------------
#DB connection
my ($url,$user,$passwd)=("DBI:mysql:database=logserver;port=3306;host=127.0.0.1","root","123qwe");
our $dbh=DBI->connect($url,$user,$passwd) or die "can't connect DB mysql ".DBI->errstr;
my $ipHead="192.168.4.";
my (@ips,@iplist):shared;
our @logtype:shared=('test');
for(my $i=1;$i<255;$i++){push(@ips,$ipHead."$i");}

my @threads;
print localtime."\n";
for(my $j=0;$j<@ips;$j++){$threads[$j]=threads->new(\&portScaner,$ips[$j]);}
foreach my $threadinstance(@threads){$threadinstance->join();}
print localtime."\n";

#---------------------------------------------------------
#scan if the port 22 is open
sub portScaner{
my ($ip)=(shift);
my $result=IO::Socket::INET->new("$ip:22");
if($result){
push(@iplist,$ip);
foreach my $type(@logtype){&getLogtypeContext($type);}

$result->close;
}
#else {writeLog("ip=$ip port 22 is not open!\n");}
}
#---------------------------------------------------------
#get filepath keywords splitwords in logtype
sub getLogtypeContext{
my ($logtype)=@_;
my $sth=$dbh->prepare("select filepath,keyword,splitword from t_logtype where type='$logtype';") or die $dbh->errstr;
$sth->execute();
my (@filepath,@keyword,@splitword);
while(my $ref=$sth->fetchrow_hashref())
{
        push(@filepath,split(",",$ref->{filepath}));
        push(@keyword,split(",",$ref->{keyword}));       
        push(@splitword,split(",",$ref->{splitword}));             
}
return (\@filepath,\@keyword,\@splitword);
}
我现在想在多线程中使用DBI查询,dbh怎么设置shared呢,我现在这个提示无法shared?谢谢

回复 4# shijiang1130


   

论坛徽章:
0
7 [报告]
发表于 2013-04-29 12:28 |只看该作者
Perl的threads问题多,不稳定,速度也慢。楼主不如考虑用Coro(Perl的多线程)

论坛徽章:
0
8 [报告]
发表于 2013-04-29 14:04 |只看该作者
谢谢,求写个简单的多线程示例!
回复 7# anyevent


   

论坛徽章:
0
9 [报告]
发表于 2013-04-29 15:36 |只看该作者

论坛徽章:
0
10 [报告]
发表于 2013-04-29 20:21 |只看该作者
谢大神!学习了。{:3_200:} 回复 9# anyevent


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP