简体   繁体   中英

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. 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. 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 ; however, it's not safe to use within the signal handler according to the GNU documentation . 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.

A blocking read on a pipe is async-signal-safe , and provides a convenient way to wake up your "suspended" thread.

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);

  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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