- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我只是在 Mac OS X 中玩信号。
为什么在我的信号处理程序完成后,以下代码不会产生 SIGSEGV 的默认行为?在 Linux 下,代码运行良好。
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void err_crash();
void my_signal_handler (int signal)
{
fprintf(stderr, "signal == %i\n", signal);
fflush(stderr);
err_crash();
}
void err_crash()
{
static int count= 0;
if (count)
signal(SIGSEGV, SIG_DFL); /* break recursion loop if we recursed */
count++;
// one of the two should really crash ;)
((void)(*((int volatile *)NULL)));
*((int *)NULL) = 42;
abort(); /* in case we're not crashed yet... */
}
int main ()
{
signal(SIGSEGV, my_signal_handler);
err_crash();
return 0;
}
编辑:我得到的输出如下:
bonecrusher:devel sw$ g++ signal_problems.cpp -o foo
bonecrusher:devel sw$ ./foo
signal == 11
^C
bonecrusher:devel sw$
问题是我希望程序在 signal == 11
输出后终止,但它会一直运行,我不得不中断它。
最佳答案
这实际上让我的大脑卡住了几分钟,而在这个时代永远不应该使用 signal()
的原因只会让我更加坚定。
首先,来自 signal()
的手册页
The behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead.
再往下:
- If the disposition is set to a function, then first either the disposition is reset to SIG_DFL, or the signal is blocked (see Portability below), and then handler is called with argument signum. If invocation of the handler caused the signal to be blocked, then the signal is unblocked upon return from the handler.
在最初的 Unix 系统中,当安装处理程序时,配置被重置为 SIG_DFL,不阻止相同类型的传入信号,然后它运行处理函数。 System V 提供了这一点,linux 内核也提供了这一点。
也就是说,一旦代码在linux系统上运行,一旦调用第二个异常,就会直接退出。
现在到了有趣的部分。 BSD 试图改善这种行为。再次从手册页:
On BSD, when a signal handler is invoked, the signal disposition is not reset, and further instances of the signal are blocked from being delivered while the handler is executing.
并且由于 mac osx 部分基于 BSD,一旦代码在 mac osx 上运行,一旦调用第二个异常,它将处于pending并等待第一个异常的处理程序导出。但由于您永远不会退出,因此您陷入了僵局。
这就是为什么应该使用 sigaction()
而不是 signal()
的原因。
现在有一些提示:
handlers 应该很短,并且返回很快。如果您正在执行计算并调用其他函数,则您可能做错了什么。信号不能替代事件驱动框架。
调用非异步安全的函数是不好的。考虑一下如果在调用 fprintf
期间发生异常并且在处理程序内部再次调用 fprintf
会发生什么情况。信号处理程序和程序数据都可能被破坏,因为它们对流本身进行操作。
关于c++ - 信号处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4863420/
我只是在 Mac OS X 中玩信号。 为什么在我的信号处理程序完成后,以下代码不会产生 SIGSEGV 的默认行为?在 Linux 下,代码运行良好。 #include #include #in
信号是由操作系统传给进程的中断,会提早终止一个程序。在 UNIX、LINUX、Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断。 有些信号不能被程序捕获,但是下表所
C、C++ 程序中,信号常常作为进程间通信的一种重要手段。举个例子: #include <stdio.h> #include <unistd.h> #include <
是否有关于重叠处理的任何好的(如果可能的话)科学资源(网络或书籍)。我对分析信号时使用重叠处理和窗口的效果不感兴趣,因为要求是不同的。有关以下实时情况的更多信息:(我当前正在处理音频信号) 将信号分成
在下面的代码中: #include #include #include #include #include using namespace std; void signal_callback_
我是操作系统的新手,在学习 Linux 信号处理时遇到了一些问题。 在 Linux 中,如果信号在系统调用期间到达,例如 pause(),程序将切换到内核模式并运行信号处理程序。然后根据处理函数,程序
我正在尝试在 shell 中实现一个简单的两级管道。 当我不做第二个 fork 而只是在父级中执行管道的其余实现时,它工作正常但我退出了 shell。这就是为什么我想做第二个 fork,这样我就不会退
我写了一个小脚本,用 Python 做了一些非常耗时的事情,我包含了一个信号处理模块,用于监听 SIGINT、SIGQUIT 和 SIGINFO,当用户输入 SIGQUIT 或 SIGINFO 时打印
我有一个正在读取的传感器,目前代码是用 Java 编写的,但我认为问题不是特定于语言的,更多的是与方法相关。 传感器会产生具有高脉冲和低脉冲的信号,大致类似于心跳。然而,“高”脉冲并不总是相同的电平,
使用mpirun时,是否可以在运行的代码中捕获信号(例如,^C生成的SIGINT)? 例如,我正在运行并行化的 Python 代码。在单独运行 python blah.py 时,我可以 except
如果 linux 进程正在等待 I/O(即它处于 SLEEP 状态)并且针对它发出 SIGKILL 信号,则在终止时( STOPPED 状态)是否会通过RUNNING 或READY 状态? 换句话说,
当进程正在执行阻塞系统调用时,比如读或写,信号已经到达。系统调用是否因错误 EINTR 而终止?处理系统调用后是否重启系统调用? 假设系统调用因错误 EINTR 终止,内核在返回用户空间之前处理该信号
有人可以解释为什么我们不应该从信号处理程序调用非异步函数吗?就像调用此类函数时破坏程序的确切步骤顺序一样。而且,信号是否总是在单独的堆栈上运行?如果是这样,它是一个单独的上下文还是在信号线程的上下文中
我在处理多线程服务器中的信号处理时遇到了麻烦。我为每个连接创建一个线程,但我希望有一个选项可以使用 SIGINT 终止服务器。但是,当其中一个线程捕获到信号时,事情会变得很糟糕。除了主线程之外,有什么
我写了一个扭曲的程序来处理来自 TCP 套接字和原始套接字的请求。由于 twisted 不支持原始线程,我在名为“raw_socket_loop”的函数中编写了原始套接字选择轮询循环。主 react
这个问题在这里已经有了答案: c: catch a segfault in exec() which was run in a child process (1 个回答) 关闭 6 年前。 我有一个
我有一个 repeat python 函数和一个只有一个按钮的 test.ui。我的疑问是每次单击按钮时如何准确地循环一次相同的功能。因为对我来说,每当我表演时: self.pushButton.cl
我想禁用像 SIGINT 这样的信号,它是通过按 CTRL_C 发送的,还有其他将终止 JVM 的信号。我在此处阅读了有关-Xrs 选项的信息 http://docs.oracle.com/javas
这个问题在这里已经有了答案: Getting fault address that generated a UNIX signal (2 个答案) 关闭 5 年前。 有没有办法找出被某些信号中断的机
是否可以通过 Erlang 中的处理程序捕获 Linux 信号(例如 SIGUSR1)? (无需求助于用 C 编写的驱动程序) 最佳答案 (不是真正的答案) 2001 年有人问: Does anyon
我是一名优秀的程序员,十分优秀!