SIGCHLD在Linux上的SIGCONT上发送,但在macOS上不发送 [英] SIGCHLD is sent on SIGCONT on Linux but not on macOS
问题描述
在主要过程中,我听SIGCHLD:
In the main process I listen to SIGCHLD:
signal(SIGCHLD, &my_handler);
然后我fork()
,execv()
并使其在后台运行(例如,/bin/cat).
Then I fork()
, execv()
and let it run in background (/bin/cat for example).
当我尝试从终端将SIGSTOP发送到子进程时,会调用my_handler()
.但是,当我尝试向其发送SIGCONT时,该处理程序未在macOS上调用,而是在我的Ubuntu上执行.
When I try from terminal to send SIGSTOP to the child process, my_handler()
gets called. But when I try to send SIGCONT to it, the the handler isn't called on macOS but it's executed on my Ubuntu.
男人:
SIGCHLD:子状态已更改.
SIGCHLD: child status has changed.
我错过了什么吗?这是预期的行为吗?我在Ubuntu上编写了我的应用程序,并期望它也可以在Mac上运行.
Am I missing something? Is it an expected behaviour? I wrote my app on Ubuntu and expected it to work on mac as well.
我也尝试过使用sigaction()
,但结果相同.
I tried with sigaction()
as well, but with the same results.
下面是一个示例代码来演示:
Here's a sample code to demonstrate:
#include <signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void my_handler(int signum)
{
printf("\t SIGCHLD received\n");
fflush(stdout);
}
void my_kill(pid_t pid, int signum)
{
printf("Sending %d\n", signum);
fflush(stdout);
kill(pid, signum);
printf("Sent %d\n\n", signum);
fflush(stdout);
}
int main()
{
pid_t pid;
char *cat_args[2] = {"/bin/cat", NULL};
signal(SIGCHLD, &my_handler);
pid = fork();
if (pid == 0)
{
execv("/bin/cat", cat_args);
}
else
{
my_kill(pid, SIGSTOP);
my_kill(pid, SIGCONT);
wait(NULL);
}
return 0;
}
在macOS上的输出:
With the output on macOS:
Sending 17
SIGCHLD received
Sent 17
Sending 19
Sent 19
推荐答案
该行为是可选的.实现无需在继续时生成SIGCHLD. POSIX.1-2008(2016版)中使用的语言是可以"而不是应该":
That behavior is optional. An implementation need not generate a SIGCHLD upon continuation. The language used in POSIX.1-2008 (2016 edition) is "may" rather than "shall":
当继续停止的进程时,除非其父进程设置了SA_NOCLDSTOP标志,否则可能为其父进程生成SIGCHLD信号 .
...只要其停止的子进程中的任何一个继续执行,就会为调用进程生成SIGCHLD信号 .
-
System Interfaces sigaction
"Description"
添加了重点.
这篇关于SIGCHLD在Linux上的SIGCONT上发送,但在macOS上不发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!