linux 中的进程终止通知

process kill notification in linux

你好,我在 Linux 中有两个进程,比如 P1 和 P2,它们都是单独的可执行文件。 在进程 "P1" 中,我正在使用 "system()" 函数来 运行 进程 "P2"。 处理 "P2" 运行s 6 秒,然后它调用 "exit()" API(它会自杀)。

我想知道如何在进程 "P1" 中获取进程 "P2" 终止通知?

这很简单 我刚刚发现“int kill (pid_t pid, int signum)”会起作用。

此功能的详细信息如下 - http://www.gnu.org/software/libc/manual/html_node/Signaling-Another-Process.html

你做错了。

是的,运行 system() 而不是在如此创建的进程上使用 kill 进行轮询,但正确的做法是使用 P1 的 fork() 和 exec() 创建 P2,这将使 P1 P2 的父进程 - 这意味着当 P2 退出时您将收到 SIGCHLD 信号。

当 P2 执行时,P1 将被阻塞在 system() 中。 P2 的终止将解锁 P1。 P1 获取 P2 的退出代码作为系统的 return 值。

I want to know how I can get process "P2" kill notification in Process "P1"?

P1 将在 P2 执行时锁定。

我建议使用 P1 中的 fork()exec()。这将使 P1 成为 P2 的主要进程。当 P2 完成执行时,它将解锁 P1 并传递 return 值。