简体   繁体   English

在信号处理程序中挂起线程执行

[英]Suspending thread execution within signal handler

This is a bit of a follow-up to this question . 这是对该问题的后续措施。

Suppose I use sigaction to set up a signal handler of my own design. 假设我使用sigaction来设置自己设计的信号处理程序。 In this signal handler, I access information about the CPU state (the thread's stack pointer, for example) and copy it to a predetermined location in memory. 在此信号处理程序中,我访问有关CPU状态的信息(例如,线程的堆栈指针),并将其复制到内存中的预定位置。 I now want to examine a value on this thread's stack from another thread. 现在,我想检查另一个线程在该线程堆栈上的值。

Is there a way to suspend execution of the thread in the signal handler from within the same signal handler, so that I can safely examine this thread's stack from another thread? 有没有办法从同一信号处理程序中挂起信号处理程序中线程的执行,以便我可以安全地从另一个线程检查该线程的堆栈? (Note: I define "safely" here to mean "without worrying about the thread completing or returning from a function.) (注意:我在这里将“安全”定义为“不必担心线程完成或从函数返回。”)

If I was outside of the signal handler, I could use sigsupend ; 如果我不在信号处理程序之外,则可以使用sigsupend ; however, it's not safe to use within the signal handler according to the GNU documentation . 但是,根据GNU文档,在信号处理程序中使用它并不安全。 I could also try extending a signal using the method described in this question , but I don't think any of the standard *nix signals will help me here. 我也可以尝试使用此问题中描述的方法扩展信号,但我认为任何标准* nix信号都不会对我有所帮助。

A blocking read on a pipe is async-signal-safe , and provides a convenient way to wake up your "suspended" thread. 管道上的阻塞read异步信号安全的 ,并提供了一种方便的方式来唤醒“挂起”的线程。

Eg, 例如,

static int the_pipe[2] = {-1, -1};  // file descriptors allocated in main via pipe()

static void
siginfo_handler(int s, siginfo_t *si, void *ctx) {
  char c;

  // do something with (ucontext_t *)ctx

  // now wait for someone else to wake us up
  read(the_pipe[0], &c, 1);

  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM