读取信号处理程序内的共享数据

发布于 2024-12-21 06:10:40 字数 185 浏览 4 评论 0原文

我所处的情况是,我需要读取信号处理程序(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若水般的淡然安静女子 2024-12-28 06:10:40

您不应该从信号处理程序访问共享数据。您可以在以下文章中找到有关信号的更多信息:

适用于应用程序程序员的 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.

纸伞微斜 2024-12-28 06:10:40

我可以看到两个非常干净的解决方案:

  1. Linux 特定的:创建一个专用的线程处理信号。使用 signalfd() 捕获信号。这样您将在常规线程中处理信号,而不是任何有限的处理程序。
  2. 便携式:还可以使用一个专用线程,该线程会休眠直到收到信号。您可以使用管道创建一对文件描述符。线程可以从第一个描述符读取(2),并且在信号处理程序中您可以将(2)写入第二个描述符。根据 POSIX。当线程从管道中读取某些内容时,它知道它必须执行某些操作。

I can see two quite clean solutions:

  1. Linux-specific: Create a dedicated thread handling signals. Catch signals using signalfd(). This way you will handle signals in a regular thread, not any limited handler.
  2. Portable: Also use a dedicated thread that sleeps until signal is received. You may use a pipe to create a pair of file descriptors. The thread may read(2) from the first descriptor and in a signal handler you may write(2) to the second descriptor. Using write() in a signal handler is legal according to POSIX. When the thread reads something from the pipe it knows it must perform some action.
落叶缤纷 2024-12-28 06:10:40

假设 SH 无法直接访问共享数据,那么也许您可以间接地做到这一点:

  1. 拥有一些只有信号处理程序可以写入的全局变量,但可以从其他地方读取(即使仅在同一线程内)。
  2. SH 在调用时设置该标志。
  3. 当线程不在修改 BST 的过程中时,线程会轮询该标志;当 find it 设置时,它们会执行原始信号所需的处理(使用任何必要的同步),然后发出不同的信号(如 SIGUSR1)以指示处理已完成
  4. 该信号的 SH 会重置标志

如果您担心 SIGSEGV 重叠,请在混合中添加一个计数器以进行跟踪。 (嘿!你刚刚构建了自己的信号量!)

这里的薄弱环节显然是轮询,但它是一个开始。

Assuming the SH can't access the shared data directly, then maybe you could do it indirectly:

  1. Have some global variable that only signal handlers can write to, but can be read from elsewhere (even if only within the same thread).
  2. SH sets the flag when it is invoked
  3. Threads poll this flag when they are not in the middle of modifying the BST; when the find it set, they do the processing that is required by the original signal (using whatever synchronizations are necessary), and then raise a different signal (like SIGUSR1) to indicate that the processing is done
  4. The SH for THAT signal resets the flag

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.

宛菡 2024-12-28 06:10:40

您可能会考虑 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 only futex and atomic instructions).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文