如何在Perl中使用不同的脚本制作fork? [英] How can I make fork in Perl in different scripts?
问题描述
我在Perl中有一个进程,该进程使用系统命令创建另一个进程,将其保留在内存中,并传递一些像这样的变量:
I have a process in Perl that creates another one with the system command, I leave it on memory and I pass some variables like this:
my $var1 = "Hello";
my $var1 = "World";
system "./another_process.pl $var1 $var2 &";
但是系统命令只返回结果,我需要获取PID.我想制作叉子之类的东西.我该怎么办?如何在不同的脚本中制作类似fork的东西?
But the system command only returns the result, I need to get the PID. I want to make something like fork. What should I do? How can I make something like fork but in diferent scripts?
提前谢谢!
推荐答案
Perl具有 fork
函数.
Perl has a fork
function.
请参见 perldoc perlfaq8
-如何在后台启动进程?
See perldoc perlfaq8
- How do I start a process in the background?
(由brian d foy贡献)
(contributed by brian d foy)
没有唯一的方式来运行代码
在后台,所以您不必
等待它完成之前
程序继续执行其他任务.
流程管理取决于您
特定的操作系统,还有许多
的技术是在perlipc中.
几个CPAN模块可能能够
帮助,包括
IPC::Open2
或者
IPC::Open3
,
IPC::Run
,
Parallel::Jobs
,
Parallel::ForkManager
,
POE
,
Proc::Background
, 和
Win32::Process
.
There's not a single way to run code
in the background so you don't have to
wait for it to finish before your
program moves on to other tasks.
Process management depends on your
particular operating system, and many
of the techniques are in perlipc.
Several CPAN modules may be able to
help, including
IPC::Open2
or
IPC::Open3
,
IPC::Run
,
Parallel::Jobs
,
Parallel::ForkManager
,
POE
,
Proc::Background
, and
Win32::Process
.
您可能还有许多其他模块 使用,所以检查那些名称空间 还有其他选择.如果您正在 类似Unix的系统,您也许可以 通过系统调用摆脱困境 放置&在命令末尾:
There are many other modules you might use, so check those namespaces for other options too. If you are on a Unix-like system, you might be able to get away with a system call where you put an & on the end of the command:
system("cmd &")
您也可以尝试使用
fork
,
如中所述
perlfunc
(尽管这是同一件事,
许多模块都可以为您服务).
You can also try using
fork
,
as described in
perlfunc
(although this is the same thing that
many of the modules will do for you).
主要流程和 有背景的人(孩子"过程) 共享相同的STDIN,STDOUT和 STDERR文件句柄.如果两者都尝试 立刻访问它们,奇怪的事情 可以发生.您可能要关闭或 重新为孩子打开这些.你可以 通过打开管道来解决这个问题 (请参见打开),但是在某些系统上,这 表示子进程不能 比父母长寿.
Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with opening a pipe (see open) but on some systems this means that the child process cannot outlive the parent.
您必须赶上SIGCHLD
信号,也可能是SIGPIPE.
SIGCHLD在后台发送
过程完成. SIGPIPE在以下情况下发送
您写入一个文件句柄,其孩子
进程已关闭(未捕获
SIGPIPE可能导致您的程序
默默地死掉).这不是问题
与system("cmd&")
.
You'll have to catch the SIGCHLD
signal, and possibly SIGPIPE too.
SIGCHLD is sent when the backgrounded
process finishes. SIGPIPE is sent when
you write to a filehandle whose child
process has closed (an untrapped
SIGPIPE can cause your program to
silently die). This is not an issue
with system("cmd&")
.
您必须准备收获" 子进程完成时. $ SIG {CHLD} = sub {wait}; $ SIG {CHLD} ='IGNORE';你也可以 用双叉.你马上 等待您的第一个孩子,然后 初始化守护进程将为您的wait() 孙子退出后.
You have to be prepared to "reap" the child process when it finishes. $SIG{CHLD} = sub { wait }; $SIG{CHLD} = 'IGNORE'; You can also use a double fork. You immediately wait() for your first child, and the init daemon will wait() for your grandchild once it exits.
unless ($pid = fork) {
unless (fork) {
exec "what you really wanna do";
die "exec failed!";
}
exit 0;
}
waitpid($pid, 0);
查看信号
perlipc
有关执行此操作的其他代码示例.
僵尸不是问题
system("prog &")
.system("prog &")
.
See Signals in
perlipc
for other examples of code to do this.
Zombies are not an issue with
system("prog &")
.system("prog &")
.
这篇关于如何在Perl中使用不同的脚本制作fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!