Is it possible to find out the number of signals which are in the signal mask. If so, how?

Dani AI

Generated

As suggested, the practical way on Linux (OpenSUSE/gcc) is to read the current sigset_t and test each signal. In multithreaded programs use pthread_sigmask (signal masks are per-thread); in single-threaded code sigprocmask will do. To count blocked signals, loop signal numbers from 1 up to NSIG-1 and use sigismember() for each one.

Here is a minimal example that works on glibc/gcc:

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

static int count_masked_signals(const sigset_t *set)
{
    int count = 0;
    for (int sig = 1; sig < NSIG; ++sig) {
        if (sigismember(set, sig) == 1)
            ++count;
    }
    return count;
}

int main(void)
{
    sigset_t mask;
    if (pthread_sigmask(SIG_BLOCK, NULL, &mask) != 0) {
        perror("pthread_sigmask");
        return 1;
    }
    printf("blocked signals: %d\n", count_masked_signals(&mask));
    return 0;
}

Notes and troubleshooting: sigismember returns -1 for invalid signal numbers, so handle that if your loop range is nonstandard. Remember masks are per-thread; calling this in one thread shows that thread's mask only. To inspect pending-but-blocked signals use sigpending() instead. See the POSIX/Linux man pages for details: pthread_sigmask manual and sigprocmask manual.

Recommended Answers

All 3 Replies

Not portably. If you want to know about non-portable methods, tell us your OS and compiler.

the OS is OpenSUSE 10.3 and the compiler gcc c++.

Look up sigprocmask.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.