当前位置:首页 > 操作系统 > MacOs

SIGCHLD在Linux上的SIGCONT上发送,但在macOS上不发送

在主要过程中,我听SIGCHLD:

<code>signal(SIGCHLD, &my_handler);
</code>

然后,我fork(),execv()并使其在后台运行(例如,/ bin / cat).

当我从终端尝试将SIGSTOP发送到子进程时,将调用my_handler().但是,当我尝试向其发送SIGCONT时,该处理程序未在macOS上调用,而是在我的Ubuntu上执行.

男人:

SIGCHLD: child status has changed.

我想念什么吗?这是预期的行为吗?我在Ubuntu上编写了我的应用程序,并期望它也可以在Mac上运行.

我也尝试了sigaction(),但结果相同.

这是一个示例代码来演示:

<code>#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 receivedn");
    fflush(stdout);
}

void    my_kill(pid_t pid, int signum)
{
    printf("Sending %dn", signum);
    fflush(stdout);

    kill(pid, signum);

    printf("Sent %dnn", 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;
}
</code>

在macOS上的输出:

<code>Sending 17
         SIGCHLD received
Sent 17

Sending 19
Sent 19
</code>

解决方法:

该行为是可选的.实现无需在继续时生成SIGCHLD. POSIX.1-2008(2016版)中使用的语言是“可以”而不是“应该”:

When a stopped process is continued, a SIGCHLD signal may be generated for its parent process, unless the parent process has set the SA_NOCLDSTOP flag.

–System Interfaces, 2.4.3 Signal Actions

…a SIGCHLD signal may be generated for the calling process whenever any of its stopped child processes are continued.

–
 System Interfaces sigaction “Description”

重点已添加.


【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐