"杀-15 QUOT;触发的sigaction code,但不会终止我的C程序 [英] "kill -15" triggers the sigaction code but does not terminate my C program

查看:137
本文介绍了"杀-15 QUOT;触发的sigaction code,但不会终止我的C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C.发展我加入到这一程序的sigaction处理序执行一些C code之前退出程序的程序:

I have a program developed in C. I added to this program a sigaction handler inorder to execute some C code before quit the program:

void signal_term_handler(int sig)
{
    printf("EXIT :TERM signal Received!\n");
    int rc = flock(pid_file, LOCK_UN | LOCK_NB);
    if(rc) {
        char *piderr = "PID file unlock failed!";
        fprintf(stderr, "%s\n", piderr);
        printf(piderr);
    }
    exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
    struct sigaction sigint_action;

    sigint_action.sa_handler = &signal_term_handler;
    sigemptyset(&sigint_action.sa_mask);

    sigint_action.sa_flags = SA_RESETHAND;
    sigaction(SIGTERM, &sigint_action, NULL);
        ...........
}

请注意:我的程序中包含2子线程运行

Note: My program contains 2 subthreads running

当我执行myprogram然后我称之为杀-15℃; pidnumber> 来杀了我的计划。我得到的消息EXIT:TERM信号接收\\ n 打印在标准输出,但该程序没有退出

When I execute myprogram and then I call kill -15 <pidnumber> to kill my program. I get the message "EXIT :TERM signal Received!\n" printed in the stdout but the program is not exited.

我失去了成才在我的sigaction code?

Am I missing someting in my sigaction code?

推荐答案

退出()不一定是异步信号安全的。

exit() is not necessarily async-signal safe.

要直接从信号处理函数调用结束进程要么 _exit()中止()

To end a process directly from a signal handler call either _exit() or abort().

羊群()的printf 系列函数的所有成员都没有异步信号救不

flock() and all members of the printf family of functions aren't async-signal-save either.

有关的异步信号安全功能的全面列表中,您可能想点击这里

For full list of async-signal-safe functions you might like to click here.

这篇关于&QUOT;杀-15 QUOT;触发的sigaction code,但不会终止我的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆