Linux SIGCHLD 信号处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9845991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:22:39  来源:igfitidea点击:

SIGCHLD Signal Processing

clinuxunixsignalssigchld

提问by darksky

In Unix, when a child process in background terminates, it sends a SIGCHLDsignal to the parent to inform it that it terminated.

在 Unix 中,当后台的子进程终止时,它会向SIGCHLD父进程发送信号以通知它已终止。

Does the same happen even if the process was in foreground? If so, this means the parent will just ignore it.

即使进程在前台也会发生同样的情况吗?如果是这样,这意味着父母将忽略它。

Is this right? Or if it is in foreground, then no signal is sent at all?

这是正确的吗?或者如果它在前台,那么根本不发送信号?

采纳答案by jim mcnamara

background and foreground are job control concepts, and are part of the the shell. They are applied to processes and do not affect which process spawned (exec-ed) another process.

background 和foreground 是作业控制概念,是shell 的一部分。它们应用于进程并且不影响哪个进程产生(执行)另一个进程。

A child process is the result of a fork()-exec() call. The child gets a parent pid of the process that executed the fork() call. This is the context of the SIGCHLD signal, the parent pid receives the SIGCHLD signal. It does not matter whether the child process is "foreground" or "background", only the ppid matters on exit of the process.

子进程是 fork()-exec() 调用的结果。子进程获取执行 fork() 调用的进程的父进程 pid。这是 SIGCHLD 信号的上下文,父 pid 接收 SIGCHLD 信号。子进程是“前台”还是“后台”并不重要,只有进程退出时 ppid 才重要。

回答by P.P

There's no such thing as foreground child.The term background process is used to simply refer that we are mainly dealing with the parent process (which may create a child processes to do a part its job). When a child process exits SIGCHLDis always sent to parent process. However, the parent process usually ignores it. If parent wants to deal with exit of child Or to do some action only after the exit of child, then it can use the wait()system call to get the status of child process.

没有前景孩子这样的东西术语后台进程用于简单地指我们主要处理父进程(它可能会创建一个子进程来完成它的一部分工作)。当子进程退出时SIGCHLD总是发送到父进程。但是,父进程通常会忽略它。如果父进程想要处理子进程退出或者子进程退出后才做一些动作,那么可以使用wait()系统调用来获取子进程的状态。