1

我有一个多线程(pthreads)应用程序,我已经挂钩SIGINT以允许我中断程序。我像这样设置了一个信号处理程序线程:

    /*** startup code ***/
// Prep signal-related stuff:
signal(SIGPIPE, SIG_IGN);

sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
assert(0 == pthread_sigmask(SIG_BLOCK, &set, NULL));

// Spawn signal handling thread
pthread_t sig_pthread;
pthread_create(&sig_pthread, NULL, &sig_thread, (void *) &set);

信号处理线程在这里:

static void* sig_thread(void *arg) {
    sigset_t *set = (sigset_t *) arg;
    int s, sig;

#ifndef __APPLE__
    prctl(PR_SET_NAME, "signal hander", 0, 0, 0);
#endif

    for (;;) {
        s = sigwait(set, &sig);
        if (s != 0) {
            ERR(E_UNKNOWN, "sigwait failed?\n");
            /* ERR is macro -> exit(E_UNKNOWN) */
        }

        if (sig == SIGINT) {
            ...
        } else {
            ERR(E_UNKNOWN, "caught unknown signal %d\n", sig);
            /* ERR is macro -> exit(E_UNKNOWN) */
        }
    }
}

正常运行时,代码按预期工作。但是,如果我在 gdb 下运行程序,我会得到:

--- E: caught unknown signal 4611

有人对(十进制)值 4611(0x1203、01103)有任何见解吗?中没有什么明显的signal.h。有谁知道 gdb 正在做什么来导致这种情况发生以及我如何修复/防止它?

4

1 回答 1

1

I suspect the 4611 to be garbage, as you do not initialise sig prior to calling sigwait().

From the sources you show, it seems like you do not skip the test of sig in case of an error (s != 0).

You might like to mod your code like so:

#include <stdio.h>    
#include <signal.h>
...

#define E_UNKNOWN "E"
#define ERR(ec, fmt, ...) (fprintf(stderr, "--- %s: " fmt, (ec), __VA_ARGS__))

...

  for (;;) {
    int sig = 0;
    int s = sigwait(set, &sig);

    if (s != 0) {
        ERR(E_UNKNOWN, "sigwait() failed with %d.\n", s);
    }
    else {
      if (sig == SIGINT) {
          ...
      } else {
        ERR(E_UNKNOWN, "Caught unhandled signal %d.\n", sig);
      }
    }
    ...
于 2013-06-18T06:13:30.210 回答