如果您不想安装该parallel
实用程序(假设它按指示工作,看起来很整洁),那么您可以调整这个 Perl 脚本(基本上,更改执行的命令),并可能减少监视:
#!/usr/bin/env perl
use strict;
use warnings;
use constant MAX_KIDS => 4;
$| = 1;
my %pids;
my $kids = 0; # Number of kids
for my $i (1..20)
{
my $pid;
if (($pid = fork()) == 0)
{
my $tm = int(rand() * (10 - 2) + 2);
print "sleep $tm\n";
# Using exec in a block on its own is the documented way to
# avoid the warning:
# Statement unlikely to be reached at filename.pl line NN.
# (Maybe you meant system() when you said exec()?)
# Yes, I know the print and exit statements should never be
# reached, but, dammit, sometimes things go wrong!
{ exec "sleep", $tm; }
print STDERR "Oops: couldn't sleep $tm!\n";
exit 1;
}
$pids{$pid} = 1;
$kids++;
my $time = time;
print "PID: $pid; Kids: $kids; Time: $time\n";
if ($kids >= MAX_KIDS)
{
my $kid = waitpid(-1, 0);
print "Kid: $kid ($?)\n";
if ($kid != -1)
{
delete $pids{$kid};
$kids--;
}
}
}
while ((my $kid = waitpid(-1, 0)) > 0)
{
my $time = time;
print "Kid: $kid (Status: $?); Time: $time\n";
delete $pids{$kid};
$kids--;
}
# This should not do anything - and doesn't (any more!).
foreach my $pid (keys %pids)
{
printf "Undead: $pid\n";
}
示例输出:
PID: 20152; Kids: 1; Time: 1383436882
PID: 20153; Kids: 2; Time: 1383436882
sleep 5
PID: 20154; Kids: 3; Time: 1383436882
sleep 7
sleep 9
PID: 20155; Kids: 4; Time: 1383436882
sleep 4
Kid: 20155 (0)
PID: 20156; Kids: 4; Time: 1383436886
sleep 4
Kid: 20152 (0)
PID: 20157; Kids: 4; Time: 1383436887
sleep 2
Kid: 20153 (0)
PID: 20158; Kids: 4; Time: 1383436889
sleep 9
Kid: 20157 (0)
PID: 20159; Kids: 4; Time: 1383436889
sleep 6
Kid: 20156 (0)
PID: 20160; Kids: 4; Time: 1383436890
sleep 6
Kid: 20154 (0)
PID: 20161; Kids: 4; Time: 1383436891
sleep 9
Kid: 20159 (0)
PID: 20162; Kids: 4; Time: 1383436895
sleep 7
Kid: 20160 (0)
PID: 20163; Kids: 4; Time: 1383436896
sleep 9
Kid: 20158 (0)
PID: 20164; Kids: 4; Time: 1383436898
sleep 6
Kid: 20161 (0)
PID: 20165; Kids: 4; Time: 1383436900
sleep 9
Kid: 20162 (0)
PID: 20166; Kids: 4; Time: 1383436902
sleep 9
Kid: 20164 (0)
PID: 20167; Kids: 4; Time: 1383436904
sleep 2
Kid: 20163 (0)
PID: 20168; Kids: 4; Time: 1383436905
sleep 6
Kid: 20167 (0)
PID: 20169; Kids: 4; Time: 1383436906
sleep 9
Kid: 20165 (0)
PID: 20170; Kids: 4; Time: 1383436909
sleep 4
Kid: 20168 (0)
PID: 20171; Kids: 4; Time: 1383436911
Kid: 20166 (0)
sleep 9
Kid: 20170 (Status: 0); Time: 1383436913
Kid: 20169 (Status: 0); Time: 1383436915
Kid: 20171 (Status: 0); Time: 1383436920