读取信号处理程序内的共享数据
我所处的情况是,我需要读取信号处理程序(SIGSEGV信号处理程序,据我所知是每个线程库)内的二叉搜索树(BST)。 BST 可以由应用程序中的其他线程修改。
现在,由于信号处理程序无法使用信号量、互斥体等,因此无法访问共享数据,我该如何解决这个问题?请注意,我的应用程序是多线程的,并且在多核系统上运行。
I am in a situation where I need to read a binary search tree (BST) inside a signal handler (SIGSEGV signal handler, which according to my knowledge is per thread base). The BST can be modified by the other threads in the application.
Now since a signal handler can't use semaphores, mutexes etc. and therefore can't access shared data, How do I solve this problem? Note that my application is multithreaded and running on a multicore system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不应该从信号处理程序访问共享数据。您可以在以下文章中找到有关信号的更多信息:
适用于应用程序程序员的 Linux 信号
Linux 信号处理模型
所有关于 Linux 信号
看起来是在 linux 中处理信号的最安全方法到目前为止是signalfd。
You shouldn't access shared data from signal handler. You can find out more information about signals in following articles:
Linux Signals for the Application Programmer
The Linux Signals Handling Model
All about Linux signals
Looks like the safest way to deal with signals in linux so far is signalfd.
我可以看到两个非常干净的解决方案:
I can see two quite clean solutions:
假设 SH 无法直接访问共享数据,那么也许您可以间接地做到这一点:
如果您担心 SIGSEGV 重叠,请在混合中添加一个计数器以进行跟踪。 (嘿!你刚刚构建了自己的信号量!)
这里的薄弱环节显然是轮询,但它是一个开始。
Assuming the SH can't access the shared data directly, then maybe you could do it indirectly:
If you're worried about overlapping SIGSEGVs, add a counter to the mix to keep track. (Hey! You just built your own semaphore!)
The weak link here is obviously the polling, but its a start.
您可能会考虑 mmap-ing fuse 文件系统(在用户空间中)。
实际上,您会对 Gnu Hurd 感到更满意,它支持 外部寻呼机
也许还有你在信号中读取二叉搜索树的技巧处理程序在实践中通常可以以不可移植且依赖于内核版本的方式工作。也许使用低级非可移植技巧来序列化访问(例如 futexes 和 原子 gcc 内置函数)可能 工作。阅读 NPTL(即当前 Linux pthread 例程)的(特定于机器的)源代码应该会有所帮助。
可能的情况是 pthread_mutex_lock 等实际上可以从 Linux 信号处理程序内部使用......(因为它可能只执行 futex 和原子指令)。
You might consider mmap-ing a fuse file system (in user space).
Actually, you'll be more happy on Gnu Hurd which has support for external pagers
And perhaps your hack of reading a binary search tree in your signal handler could often work in practice, non-portably and in a kernel version dependent way. Perhaps serializing access with low-level non portable tricks (e.g. futexes and atomic gcc builtins) might work. Reading the (machine specific) source code of NPTL i.e. current Linux pthread routines should help.
It could probably be the case that
pthread_mutex_lock
etc are in fact usable from inside a Linux signal handler... (because it probably does onlyfutex
and atomic instructions).