Perl:为什么这会创建数千个子进程?
所以当我运行这段代码时,它似乎对系统进行了分叉炸弹,你们能帮我吗?我想要做的就是为每个 appWatch 域和环境启动一个线程。
#!/usr/bin/perl
#
#
# Starts the mass processes to watch each directory & enviroment.
#
#
#
###################################################################################
use strict;
use warnings;
use POSIX 'setsid';
setsid();
my @domains = (qw(austin batman luke heman drevil joker skeltor drevil goodguy badguy));
my @envs = (qw(qa dev));
foreach my $env (@envs){
foreach my $guy (@domains){
unless(my $pid = fork()){
system("echo $env.$guy");
system("sleep 10 ");
#system("./appWatch -d $guy -e $env");
open PID, ">>pid.lock";
print PID $$ . "\n";
print "$$ is Parent, $pid is child";
}
}
}
wait();
So when I run this code it seems to fork bomb the system can you guys help me out? All I want to do is start a thread for each one of the appWatch domains and enviroments.
#!/usr/bin/perl
#
#
# Starts the mass processes to watch each directory & enviroment.
#
#
#
###################################################################################
use strict;
use warnings;
use POSIX 'setsid';
setsid();
my @domains = (qw(austin batman luke heman drevil joker skeltor drevil goodguy badguy));
my @envs = (qw(qa dev));
foreach my $env (@envs){
foreach my $guy (@domains){
unless(my $pid = fork()){
system("echo $env.$guy");
system("sleep 10 ");
#system("./appWatch -d $guy -e $env");
open PID, ">>pid.lock";
print PID $ . "\n";
print "$ is Parent, $pid is child";
}
}
}
wait();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码应该只创建三个子级。如果您看到创建了一堆子项,那么您正在运行不同的代码(或者罪魁祸首是
appWatch
而不是您的代码)。稍微不相关的一点是,有几件事您可能应该采取不同的做法:fork
有三个可能的返回值,而不是两个,exec
而不是 system 如果您不想返回代码,system
和exec
的多参数版本如果您不希望 shell 对参数执行某些操作,请选择单参数版本。这是我的代码版本:
您更新的代码版本显示了发生了什么。您的孩子不会退出。这是我编写代码的方式:
Your code should only create three children. If you are seeing a bunch of children being created then you are running different code (or the culprit is
appWatch
not your code). On a slightly unrelated note, there are a couple things you should probably be doing differently:fork
has three possible return values, not twoexec
instead of system if you don't want to return to the codesystem
andexec
instead of the one argument version if you don't want the shell to do stuff with the arguments.Here is my version of your code:
You updated version of the code shows what happened. Your child does not exit. Here is how I would write your code:
后
运行
,我没有得到
fork
炸弹,只是一个令人兴奋的笛卡尔积:With
running on
I get no
fork
bomb, just an unexciting Cartesian product: