我如何在 Perl 中生成一个进程,而不会在父进程退出时使其失效或成为僵尸?
我有一个 perl 脚本,我想生成一个进程。这可能需要一段时间,并且大多数时候父脚本将退出。我如何生成这个进程,以便当父进程消失时,它不会在完成后变成僵尸或失效进程?
编辑:我想我找到了两种方法。希望有人能告诉我哪一个更合适?
- 设置 $SIG{CHLD} = 'IGNORE';
- 使用 POSIX 'setsid';
编辑:生成的进程也将是另一个 perl 脚本。
I have a perl script which i'd like to spawn a process. It can take a while and most times the parent script will exit. How do I spawn this process so that when the parent is gone it wont turn into a zombie or a defunct process when its done?
edit: I think ive found two methods. Hopefully someone could tell me which one is more appropriate?
- setting $SIG{CHLD} = 'IGNORE';
- use POSIX 'setsid';
edit: The spawned process is also going to be another perl script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当进程退出并且其父进程通过
wait()
获取其状态之前,该进程将变为僵尸进程。当一个进程派生另一个进程然后退出时,子进程将成为 pid 1(经典的“init”)的父进程,后者立即获取进程状态。因此,通常问题与您所描述的相反:子进程变成僵尸(因为父进程不是为处理SIGCHLD
并调用wait()
而编写的),但是当父级退出僵尸由init
继承并立即收获。事实上,将子进程与其父进程完全解耦(“守护进程”)的常用解决方案涉及有意分叉并让中间进程退出,以便守护进程立即成为 init 的子进程。编辑:如果您在 shell 中并且想要实现此效果,请尝试
(subprocess &)
。括号创建一个子shell,它在后台执行subprocess
,然后立即退出。A process becomes as zombie when it exits and before its parent process picks up its status with
wait()
. When one process forks another and then exits, the child becomes a parent of pid 1 (classically "init") which immediately reaps the process state. So usually the problem is the reverse of what you describe: The child becomes a zombie (since the parent was not written to deal withSIGCHLD
and callwait()
) but when the parent exits the zombie is inherited byinit
and immediately reaped. In fact, the usual solution to decouple a child process fully from its parent ("daemonize") involves intentionally forking and having the intermediate process exit so that the daemon is immediately a child ofinit
.Edit: If you're in shell and want to achieve this effect, try
(subprocess &)
. The parenthesis create a subshell which executessubprocess
in the background and then immeidately exits.