2013-05-02 46 views
1

在UNIX系统如何在不阻止的情况下顺序启动多个程序?

我有一个名为program_sets目录,并在program_sets,存在8目录,每个目录,他们有一个叫做A.pl

我要启动和运行8 A.程序pl程序,但是当我启动第一个程序时,程序将被阻塞,直到第一个程序调用完成。我该如何解决这个问题?

这里是我的代码

#!/usr/bin/perl 

opendir(Programs,"./program_sets"); 
@Each_names = readdir(Programs); 
shift(@Each_names); 
shift(@Each_names); 

for($i=0;$i<=$#Each_names;$i++) 
{ 
    `perl ./program_sets/$Each_names[$i]/A.pl`; 
} 

感谢

+0

的可能重复[在Perl中,我怎么能阻止的了一堆系统调用来完成?](http://stackoverflow.com/questions/2231833/in-perl-how-can-i-块一堆的系统调用完成) – Thilo 2013-05-02 03:26:54

+0

@Thilo不,这个问题是关于如何等待,他不想等待。 – Barmar 2013-05-02 03:28:42

+0

在* n * x或Windows中运行? – bugmagnet 2013-05-02 03:31:32

回答

1

&它们运行在后台,就像你从shell会。

for($i=0;$i<=$#Each_names;$i++) 
{ 
    system("perl ./program_sets/$Each_names[$i]/A.pl >/dev/null 2>&1 &"); 
} 

此外,反引号应该当你分配输出到一个变量中。使用system()运行命令而不保存输出。

0

有看起来是其他一些问题在这里。

#!/usr/bin/perl 

# warnings, strict 
use warnings; 
use strict; 

# lexically scoped $dh 
#opendir(Programs,"./program_sets"); 
my $cur_dir = "./program_sets"; 
opendir(my $dh, $cur_dir); 

# what exactly is being shifted off here? "." and ".."?? 
#@Each_names = readdir(Programs); 
#shift(@Each_names); 
#shift(@Each_names); 

# I would replace these three lines with a grep and a meaningful name. 
# -d: only directories. /^\./: Anything that begins with a "." 
# eg. hidden files, "." and ".." 
my @dirs = grep{ -d && $_ !~ /^\./ } readdir $dh; 
close $dh; 

for my $dir (@dirs) { 
    my $path = "$cur_dir/$dir"; 

    system("perl $path/A.pl >/dev/null 2>&1 &"); 
} 
+0

是的,我换班了。和.. – user2131116 2013-05-02 05:43:31

+0

@ user2131116 - 即使这种方式工作,最后我检查了'readdir'没有排序,这是用2''shift'做错误的方法。如果您将任何其他文件添加到包括“隐藏”文件的目录中,这也会失败。查看我通过更改留下的评论。 – chrsblck 2013-05-02 05:48:49

+0

如果我对readdir数组进行排序,然后移动head的两个元素,那么它必须移位。或者..对吗? – user2131116 2013-05-02 09:41:48

相关问题