2

I did something wrong in my code, where an other process send a SIGUSR2 signal to it:

sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGILL);
sigaddset(&sigset, SIGUSR2);
sigwait(&sigset, &received);

XCode notices SIGUSER2(31) signal received, but received = SIGILL(4) (or the minimal signal in the set).

Why is that so? Where I am wrong?

Now, it looks like this:

    sigset_t sigset;
    sigemptyset(&sigset);
    sigaddset(&sigset, SIGILL);
    sigaddset(&sigset, SIGUSR2);
    sigprocmask(SIG_BLOCK, &sigset, 0);
    sigwait(&sigset, &received);
    if(received == SIGUSR2) {
        //...
    } else if(received == SIGILL) {
        //...
    }

Still not working.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131

2 Answers2

1

As stated in the related question sigwait in Linux (Fedora 13) vs OS X, you need to block the signal using sigprocmask() (for single-threaded applications) or pthread_sigmask() (for multi-threaded applications).

Checking sigwait's return value for errors would not be bad either.

Community
  • 1
  • 1
jilles
  • 10,509
  • 2
  • 26
  • 39
  • I tried it, but with no success. I added a new line: sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, SIGILL); sigaddset(&sigset, SIGUSR2); sigprocmask(SIG_BLOCK, &sigset, 0); sigwait(&sigset, &received); Still worng. –  May 10 '11 at 22:07
  • I have checked it, it returns 0. –  May 11 '11 at 01:23
1

Sometimes the debugger can get in the way. I have seen debuggers interfere with signal handling before. Try running the code without the debugger involved.

The following code works perfectly on OS X:

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

int main()
{
    sigset_t set;
    int sig;

    sigemptyset(&set);
    sigaddset(&set, SIGUSR1);
    sigaddset(&set, SIGUSR2);
    sigprocmask(SIG_BLOCK, &set, NULL);
    sigwait(&set, &sig);
    printf("Got signal %d\n", sig);
    return 0;
}
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • @user408141: Does my code work for you? What output do you get if you build it, run it and kill -USR2 it? – Zan Lynx May 11 '11 at 01:31
  • @user408141: Also, do not run the program in the debugger. I have seen debugger and signals not work together well in the past. The debugger may be the problem. – Zan Lynx May 11 '11 at 01:32
  • Yeah, debug mode was the faulty :) Thx. [Me noob :P] –  May 11 '11 at 11:52