- 论坛徽章:
- 95
|
回复 1# bin_linux96
我寫的一個:
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <signal.h>
- #include <setjmp.h>
- sigjmp_buf main_env;
- int trigger_cnt = 1;
- int segv_cnt = 1;
- void
- segv_trigger(void)
- {
- printf("SIGSEGV tigger %d.\n", trigger_cnt++);
- int *p = NULL;
- *p = 0;
- }
- void
- segv_handler(int signum)
- {
- assert(signum == SIGSEGV);
- printf("SIGSEGV has been catched %d times.\n", segv_cnt++);
- siglongjmp(main_env, 1);
- }
- int
- main(void)
- {
- struct sigaction segv_act;
- volatile int longjmp_cnt;
- segv_act.sa_handler = segv_handler;
- sigemptyset(&segv_act.sa_mask);
- segv_act.sa_flags = 0;
- if (sigaction(SIGSEGV, &segv_act, 0) < 0) {
- perror("sigaction");
- exit(EXIT_FAILURE);
- }
- longjmp_cnt = 1;
- if (sigsetjmp(main_env, 1) != 0) {
- printf("Long jump %d.\n", longjmp_cnt++);
- if (longjmp_cnt > 3)
- exit(EXIT_SUCCESS);
- }
- segv_trigger();
- /* UNREACHABLE */
- exit(EXIT_SUCCESS);
- }
复制代码 |
|