gpt4 book ai didi

c - sigwait() 在 macOS 和 Linux 中的行为是否不同?

转载 作者:行者123 更新时间:2023-12-03 00:18:42 26 4
gpt4 key购买 nike

我发现以下代码在 macOS 和 Linux 中的工作方式不同:

#include <signal.h>
#include <unistd.h>
#include <stdio.h>

void catcher( int sig ) {
printf( "Signal catcher called for signal %d\n", sig );
}

int main( int argc, char *argv[] )
{
struct sigaction sigact;
sigset_t waitset;
int sig;
int result = 0;

sigemptyset( &sigact.sa_mask );
sigact.sa_flags = 0;
sigact.sa_handler = catcher;

sigaction( SIGINT, &sigact, NULL );

sigemptyset( &waitset );
sigaddset( &waitset, SIGHUP);

result = sigwait(&waitset, &sig) ;
if(result == 0)
{
printf( "sigwait() returned for signal %d\n", sig );
}
}

当在 macOS 上运行并向进程发送 SIGINT 时,仅在发送 SIGHUP 后才执行其处理程序(从而导致 sigwait() 返回)。换句话说,它看起来 sigwait() 在等待期间阻止了其等待掩码之外的所有信号。当相同的程序在 Linux 上运行时,SIGINT 会被传递,也就是说,一旦 SIGINT 发送到进程,处理程序就会运行。因此,在 Linux 中 sigwait() 看起来不会阻塞其等待掩码之外的信号。哪个是标准行为? SUSv3没有说清楚。

最佳答案

sigwaitclearly not specified to block any signals 。如果在 MacOS X 上这样做,这似乎是一个错误。

关于c - sigwait() 在 macOS 和 Linux 中的行为是否不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com