苏格拉底问答、实践过程截图、遇到问题解决问题截图,代码链接

发布时间 2023-11-10 17:55:23作者: 20211106隋吉达










#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);

}