Coming back to life after Segmentation Violation

前端 未结 13 2198
暗喜
暗喜 2020-12-08 11:26

Is it possible to restore the normal execution flow of a C program, after the Segmentation Fault error?

struct A {
    int x;
};
A* a = 0;

a->x = 123; /         


        
13条回答
  •  情话喂你
    2020-12-08 12:21

    This glib manual gives you a clear picture of how to write signal handlers.

    A signal handler is just a function that you compile together with the rest
    of the program. Instead of directly invoking the function, you use signal 
    or sigaction to tell the operating system to call it when a signal arrives.
    This is known as establishing the handler.
    

    In your case you will have to wait for the SIGSEGV indicating a segmentation fault. The list of other signals can be found here.

    Signal handlers are broadly classified into tow categories

    1. You can have the handler function note that the signal arrived by tweaking some global data structures, and then return normally.
    2. You can have the handler function terminate the program or transfer control to a point where it can recover from the situation that caused the signal.

    SIGSEGV comes under program error signals

提交回复
热议问题