gpt4 book ai didi

c - 从信号处理程序尝试 `longjmp` 时停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:42 24 4
gpt4 key购买 nike

这是我的代码:

#include <stdio.h>
#include <setjmp.h>
#include <signal.h>

jmp_buf buf;

void handler(int s);

int main(int argc, char **argv) {
signal(SIGINT, handler);
if (setjmp(buf)) {
printf("back again!\n");
return 0;
} else {
printf("first here.\n");
}

for (;;) {}
}

void handler(int s) {
longjmp(buf, 1);
}

我在 Windows 8 64 位的 VS 2012 下编译它。每次我按下 Control+C 时,程序都不会按预期重新启动,而是停止工作。谁能帮帮我?

最佳答案

来自当前的 C 标准:

If the signal occurs other than as the result of calling the abort or raise function, the behavior is undefined if the signal handler refers to any object with static or thread storage duration that is not a lock-free atomic object other than by assigning a value to an object declared as volatile sig_atomic_t, or the signal handler calls any function in the standard library other than the abort function, the _Exit function, the quick_exit function, or the signal function with the first argument equal to the signal number corresponding to the signal that caused the invocation of the handler.

您正在使用不在允许类型列表 (buf) 中的具有静态存储持续时间的对象,并且您使用的是不在允许函数列表中的标准库函数 ( longjmp).

关于c - 从信号处理程序尝试 `longjmp` 时停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17862982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com