小编etu*_*cey的帖子

阻塞函数accept()在SIGINT发生时重新启动,即使SA_RESTART标志设置为零

我正在Linux上编写一个C程序,我有一个main()和两个由它创建的pthread.在其中一个pthreads中,我调用了accept()函数.

我有一个信号处理程序,在收到SIGINT,SIGQUIT或SIGTERM时调用.

我的期望是,因为我正在使SA_RESTART标志为零,当我按下ctrl-c时,accept()函数应返回EINTR而不是重新启动,但是我在调​​试时通过一堆printf调用实现(看看哪些行是通过打印执行的)代码所在的地方,即使我的应用程序能够捕获SIGINT,接受函数仍然被阻塞,它不会因EINTR而失败并且不会移动到下一行代码.这是我在main()中的设置

struct sigaction signal_action;
signal_action.sa_flags = 0; // Don't restart the blocking call after it failed with EINTR
signal_action.sa_handler = terminate;
sigemptyset(&signal_action.sa_mask);
sigfillset(&signal_action.sa_mask); // Block every signal during the handler is executing
if (sigaction(SIGINT, &signal_action, NULL) < 0) {
    perror("error handling SIGINT");
}
if (sigaction(SIGTERM, &signal_action, NULL) < 0) {
    perror("error handling SIGTERM");
}
if (sigaction(SIGQUIT, &signal_action, NULL) < 0) {
    perror("error handling SIGQUIT");
}
Run Code Online (Sandbox Code Playgroud)

这是信号处理程序:

void terminate (int signum)
{
    terminate_program = 1;
    printf("Terminating.\n");
}
Run Code Online (Sandbox Code Playgroud)

这是调用accept()的pthread(我试图删除不相关的东西,使我的问题更容易理解):

void* …
Run Code Online (Sandbox Code Playgroud)

c linux signals pthreads blocking

1
推荐指数
1
解决办法
749
查看次数

标签 统计

blocking ×1

c ×1

linux ×1

pthreads ×1

signals ×1