- 论坛徽章:
- 0
|
有没有人碰到过这种情况
在perl线程里执行shell命令(打比方,如cat),因为想直接获取命令打印信息,用的是``
但线程会阻塞在所执行命令那(出现概率很大)
我现在写了测试脚本,只是cat个100来K的文件,就会停在那,代码如下:
#!/usr/bin/perl -w
use strict;
use vars qw / @thrs /;
use threads;
use threads::shared;
my $thr_num = $ARGV[2] || 10; # 创建的线程数目
my $file_name = $ARGV[1]; # 线程里cat命令打开的文件
my $count:shared; # 执行命令的总次数
$count = $ARGV[0]||100;
for (1..$thr_num) {
push(@thrs, threads->new(\&thread_req));
}
for (@thrs) {
$_->join();
print $_->tid()." join\n";
}
sub thread_req {
LOOP:
while (1) {
my $id;
{
lock($count);
last LOOP if ($count == 0);
$id = $count--;
}
print "--", threads->tid(), " count=$count\n";
`cat $file_name`; # 线程会停在这
}
print "--", threads->tid(), " ok\n";
}
|
这样执行命令,test.txt大小为 80158 Byte:
[root@ns4 ~]# ./test_multi4.pl 50 test.txt 5
--1 count=49
--2 count=48
...
--5 count=0
--5 ok
--4 ok
到这就停住了
查看进程可以看到:
89-root 22149 0.0 0.2 7664 2552 ? Ss 14:50 0:01 \_ sshd: [email=root@pts/7]root@pts/7[/email]
90-root 22154 0.0 0.1 6576 1676 pts/7 Ss 14:50 0:00 \_ -bash
91:root 2130 0.0 0.4 62404 4404 pts/7 Sl+ 15:29 0:00 \_ /usr/bin/perl -w ./test_multi4.pl 50 test.txt 5
92:root 2179 0.0 0.0 5904 416 pts/7 S+ 15:29 0:00 \_ cat test.txt
93:root 2180 0.0 0.0 5320 416 pts/7 S+ 15:29 0:00 \_ cat test.txt
94:root 2178 0.0 0.0 6016 416 pts/7 S+ 15:29 0:00 \_ cat test.txt
不明白为什么会这样,似乎只要执行的shell命令有较多的打印信息,就会出现这种情况
但是单个线程的话,我试了很多遍都没出现
另外,若用system函数调用,我测了很多次也没有出现这种情况
能否帮忙看下问题所在,谢谢:)
[ 本帖最后由 iceberg77 于 2008-10-20 18:12 编辑 ] |
|