#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h> //for a long jump
jmp_buf env; //for saving lonjmp enviroment
int count = 0;
void handler(int sig , siginfo_t *siginfo , void *context)
{
printf("handler: sig=%d from PID=%d UID=%d count=%d\n",
sig , siginfo->si_pid , siginfo->si_uid , ++count);
if (count >= 4) // let it occur up to 4 times
longjmp(env , 1234);
}
int BAD()
{
int *ip = 0;
printf("in BAD(): try to dereference NULL pointer\n");
*ip = 123; // dereference a NULL pointer
printf("should not see this line\n");
}
int main(int argc , char *argv[])
{
int r;
struct sigaction act;
memset(&act , 0 , sizeof(act));
act.sa_sigaction = &handler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV , &act , NULL); //install SIGSEGV catcher
if ((r = setjmp(env)) == 0) //call set jmp(env)
BAD(); //call BAD()
else
printf("proc %d survived SEGMENTATION FAULT: r=%d\n", getpid() , r);
printf("proc %d looping\n" , getpid());
while (1);
}
标签:截图,int,printf,BAD,env,act,include,问答,苏格拉底 From: https://www.cnblogs.com/sjd-sr/p/17824700.html