如何在另一个子进程完成时终止子进程?

How to terminate child process when another child process finishes?

我的代码段如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  pid_t first, last;

  last = fork();
  if (last == 0) {
    char *args[] = { "./last", NULL };
    char *env[] = { NULL };
    execve("./last", args, env);
    _exit(1);
  } else if (last == -1) {
    fprintf(stderr, "Error: failed to fork last.\n");
    exit(1);
  }

  first = fork();
  if (first == -1) {
    fprintf(stderr, "Error: failed to fork first.\n");
    exit(1);
  } else if (first > 0) {
    int status;
    waitpid(first, &status, 0);
  } else {
    char *args[] = { "./first", NULL };
    char *env[] = { NULL };
    execve("./first", args, env);
    _exit(1);
  }

  return 0;
}

从某种意义上说,这工作得很好,我可以看到进程被调用并且 运行。但是,我的问题是进程 last 有一个无限循环,当进程 first 终止时,它仍然保持 运行。在 C 中有没有一种方法可以强制进程 last 在进程 first 完成时也终止?

kill() 应该很有趣。来自 kill() 的手册页:

#include <sys/types.h>
#include <signal.h>

int kill(pid_t pid, int sig);

由于 fork() returns child PID 在 parent 中,您可以使用它来调用 kill()。这是修改后的代码和测试 运行.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>  // Modification 1

int main(int argc, char *argv[]) {
  pid_t first, last;

  last = fork();
  if (last == 0) {
    char *args[] = { "./last", NULL };
    char *env[] = { NULL };
    execve("./last", args, env);
    _exit(1);
  } else if (last == -1) {
    fprintf(stderr, "Error: failed to fork last.\n");
    exit(1);
  }

  first = fork();
  if (first == -1) {
    fprintf(stderr, "Error: failed to fork first.\n");
    exit(1);
  } else if (first > 0) {
    int status;
    waitpid(first, &status, 0);
    kill(last, SIGINT);   // Modification 2
  } else {
    char *args[] = { "./first", NULL };
    char *env[] = { NULL };
    execve("./first", args, env);
    _exit(1);
  }

  return 0;
}

航站楼 Session(之前):

$ ls test first last 
first  last  test
$ ./test
$ ps aux | grep last
root      165130  0.0  0.0   2136   752 pts/3    S    16:58   0:00 ./last
root      165135  0.0  0.0   6136   892 pts/3    S+   16:58   0:00 grep last

航站楼 Session(之后):

$ ls test first last 
first  last  test
$ ./test
$ ps aux | grep last
root      165240  0.0  0.0   6136   836 pts/3    S+   17:01   0:00 grep last

关于要传递的信号:默认操作为终止的任何人。您可以从 signal 手册页中找到更多信息。由于我不知道 last 可执行文件中的确切内容,我假设没有为 SIGINT 注册的信号处理程序,因此当 last 获取 SIGINT 时,程序默认终止。

一种方法是使用函数 kill()

向第一个进程发送 SIGTERM 或 SIGKILL 信号

一个例子:

kill(first, SIGTERM);

当您发送该信号时,如果需要,您可以对该进程进行一些“清理”。为此,您需要捕获并处理信号。在那种情况下,我建议使用 SIGTERM。

对于信号处理,请查看 this