refer to Exceptions in C with Longjmp and Setjmp to write the following program:
-sharpinclude <setjmp.h>
-sharpinclude <signal.h>
-sharpinclude <stdio.h>
jmp_buf ex_buf__;
-sharpdefine TRY do{ if(!setjmp(ex_buf__)) {
-sharpdefine CATCH } else {
-sharpdefine ETRY } } while(0)
-sharpdefine THROW longjmp(ex_buf__, 1)
void sigint_handler(int sig) {
THROW;
}
int main(void) {
if (signal(SIGINT, sigint_handler) == SIG_ERR) {
return 0;
}
TRY {
while (1) {
printf("Run...\r");
}
/* raise(SIGINT); */
} CATCH {
printf("KeyboardInterrupt");
}
ETRY;
return 0;
}
originally hoped that when the program was running, pressing Ctrl-C
would get the output: KeyboardInterrupt
.
however, the program crashes at actual run time. The output before the crash is still Run.
.
if you replace the while
part with raise (SIGINT)
, you can get the desired result.
Why is this? Does it have anything to do with the endless cycle?
compilation environment:
-
Compiler:
gcc version 7.2.0 (x86_64-win32-seh-rev1, Built by MinGW-W64 project)
clang version 5.0.1 (tags/RELEASE_501/final) Target: x86_64-pc-windows-msvc Thread model: posix
the result of the two runs is the same.
-
operating system:
Microsoft Windows 7