Why signal handler goes to infinite loop? - SIGSEGV

前端 未结 2 1293
盖世英雄少女心
盖世英雄少女心 2020-12-18 01:08

Any idea why the signal handler goes to infinite loop?

Here is the code. Please help me.

enter code here
 9 void SIGSEGV_handler(int signal)
10 {
11          


        
2条回答
  •  Happy的楠姐
    2020-12-18 01:48

    The default action for SIGSEGV is to terminate your process. But you install a handler and override this:

    /* Does nothing to "fix" what was wrong with the faulting
     * instruction.
     */
    void SIGSEGV_handler(int signal)
    {
        printf("Segmentation fault caught....\n");
        printf("Value of instance variable: i = %d\n\n", i);
    }
    

    So for every instruction that triggers a sigsegv, this handler is called and the instruction is restarted. But your handler did nothing to fix what was wrong in the first place with the faulting instruction.

    In conclusion, when the instruction is restarted, it will fault again. And again, and again and... you get the idea.

提交回复
热议问题