2013-05-02 47 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

相關問題