简体   繁体   English

当父进程等待子进程终止时,子进程如何杀死父进程?

[英]How can child kill parent process while parent process waits until child process to terminate?

I do not understand the following code: 我不明白以下代码:

pid_t pid;
int counter = 0;
void handler1(int sig) {
  counter++;
  printf("counter = %d\n", counter);
  fflush(stdout);
  kill(pid, SIGUSR1);
}
void handler2(int sig) {
  counter += 3;
  printf("counter = %d\n", counter);
  exit(0);
}
int main() {
  signal(SIGUSR1, handler1);
  if ((pid = fork()) == 0) {
    signal(SIGUSR1, handler2);
    kill(getppid(), SIGUSR1);
    while (1) {
    };
  } else {
    pid_t p;
    int status;
    if ((p = wait(&status)) > 0) {
      counter += 2;
      printf("counter = %d\n", counter);
    }
  }
}

How can the child process actually kill the parent process while the parent process waits until its child process terminates? 在父进程等待子进程终止之前,子进程如何实际杀死父进程?

Note first that the kill() syscall is somewhat misnamed, as it is a general-purpose function for sending a signal to a process. 首先请注意, kill() syscall的名称有些错误,因为它是用于向进程发送信号的通用函数。 Killing the target process is only one of several possible results. 终止目标进程只是几种可能结果之一。

More generally, however, signal handling is is an interrupt mechanism. 但是,更一般而言,信号处理是一种中断机制。 A great many library functions, including wait() can be interrupted by receipt of a signal. 接收信号会中断许多库函数,包括wait() Control passes to the registered handler for that signal, if any, or else the default action is performed. 控制将该信号传递给已注册的处理程序(如果有),否则将执行默认操作。 Afterward, the function that was interrupted returns, indicating an error via its return code and / or by setting errno . 然后,被中断的函数返回,并通过其返回代码和/或通过设置errno指示错误。

Edited to add : Additionally, your own code may be interrupted by receipt of a signal. 编辑添加 :另外,您自己的代码可能会由于收到信号而中断。 Provided that the effect is not to terminate the process, execution will resume at the point where it left off if the signal handler exits normally, or at some other point if the handler exits by calling longjmp() . 如果效果不是终止进程,则如果信号处理程序正常退出,则执行将从中断的位置继续执行;如果信号处理程序通过调用longjmp()退出,则执行将在其他位置恢复。

Also, signal delivery is an asynchronous mechanism handled by the kernel. 同样,信号传递是内核处理的异步机制。 A process can be blocked or otherwise occupied and still receive signals; 进程可能被阻塞或以其他方式占用并仍接收信号; indeed, that's a big part of the point of them. 确实,这是他们重点的很大一部分。 Each process has a queue of pending signals awaiting handling; 每个进程都有一个等待处理的待处理信号队列; for most processes, this queue is empty almost all the time, but unless you explicitly block signals, it is never safe for a process to assume that it will not receive any (and certain signals cannot be blocked anyway). 对于大多数进程,此队列几乎一直都是空的,但是除非您显式阻止信号,否则一个进程假定它不会接收任何信号(并且始终无法阻止某些信号)永远是不安全的。

In your particular code, the main process starts by setting function1() to be its handler for signal USR1 . 在您的特定代码中,主要过程始于将function1()设置为其信号USR1处理程序。 It then forks, and the child process sets function function2() as its handler for signal USR1 (not thereby affecting the parent), sends signal USR1 to the parent, and goes into an infinite loop. 然后进行分叉,子进程将函数function2()设置为其信号USR1处理程序(因此不会影响父级),将信号USR1发送给父级,并进入无限循环。

Meanwhile, the parent initiates a wait() for its child process. 同时,父级为其子进程启动一个wait() This will be interrupted by receipt of the signal. 这将被信号接收中断。 The wait will end after the signal is received and the registered handler runs, with wait() returning -1 and setting errno to EINTR . 在接收到信号并运行注册的处理程序之后,等待将结束,其中wait()返回-1并将errnoEINTR One of the actions the handler performs is to send a SIGUSR1 to the child. 处理程序执行的操作之一是将SIGUSR1发送给子级。

Upon receiving the signal, the child's normal flow of execution is interrupted to run its handler, function2() . 收到信号后,孩子的正常执行流程将中断以运行其处理程序function2() This updates the child 's copy of variable counter , prints its value, and exit() s. 这将更新孩子的变量counter的副本,打印其值,然后exit()

End result: 最终结果:

The main process prints 主要过程打印

counter = 1 计数器= 1

then exits. 然后退出。 The child process prints 子进程打印

counter = 3 计数器= 3

then exits. 然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM