我如何在 Perl 中生成一个进程,而不会在父进程退出时使其失效或成为僵尸?

发布于 2024-10-31 09:26:04 字数 235 浏览 1 评论 0原文

我有一个 perl 脚本,我想生成一个进程。这可能需要一段时间,并且大多数时候父脚本将退出。我如何生成这个进程,以便当父进程消失时,它不会在完成后变成僵尸或失效进程?

编辑:我想我找到了两种方法。希望有人能告诉我哪一个更合适?

  1. 设置 $SIG{CHLD} = 'IGNORE';
  2. 使用 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?

  1. setting $SIG{CHLD} = 'IGNORE';
  2. use POSIX 'setsid';

edit: The spawned process is also going to be another perl script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2024-11-07 09:26:04

当进程退出并且其父进程通过 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 with SIGCHLD and call wait()) but when the parent exits the zombie is inherited by init 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 of init.

Edit: If you're in shell and want to achieve this effect, try (subprocess &). The parenthesis create a subshell which executes subprocess in the background and then immeidately exits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文