Enable a signal handler using sigaction in C

后端 未结 1 789
独厮守ぢ
独厮守ぢ 2021-01-04 07:41
  struct sigaction psa;

I have enabled my signal handler in the main function as shown below:

    memset (&psa, 0, sizeof (psa)         


        
相关标签:
1条回答
  • 2021-01-04 08:22
    #include <stdio.h>
    #include <signal.h>
    
    static void pSigHandler(int signo){
        switch (signo) {
                case SIGTSTP:
                printf("TSTP");
                fflush(stdout);
                break;
        }
    }
    
    int main(void)
    {
        struct sigaction psa;
        psa.sa_handler = pSigHandler;
        sigaction(SIGTSTP, &psa, NULL);
        for(;;) {}
        return 0;
    }
    

    Because you need to fflush(stdout)

    try with C-z

    I'm not even sure if it's safe to use stdio in a signal handler though.

    Update: http://bytes.com/topic/c/answers/440109-signal-handler-sigsegv

    According to that link, you should not do this.

    0 讨论(0)
提交回复
热议问题