Could anyone please explain in a really easy way to understand what sigemptyset() does? Why is it useful? I've read a bunch of definitions but i just don't understand. From what i gather it tracks the signals that are being used for blocking purposes? I'm not really sure i understand why that would be useful. Is it so we do not get that specific signal recursively?
Basic example where sigemptyset() is used:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
int main(){
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler=function_name;
act.sa_flags=0;
sigaction(SIGINT, &act, 0);
}
The name says it all.
sigemptyset()
empties (a variable describing) a set of signals.That is
initialises a variable of type
sigset_t
(ss
here) to "contain" no signals.From
man sigemptyset
(Linux):From the POSIX specs:
sigemptyset
simply initializes thesignalmask
to empty, such that it is guaranteed that no signal would be masked. (that is, all signals will be received) Basically it is similar to amemset(0)
but you don't have to know the details of the implementation. So if thesa_mask
member is changed you don't need to adjust your code because it will be taken care of bysigemptyset
.Take a step back.
sigset_t
is a construct that represents multiple signals upon which some action is going to be take via some system call (e.g.sigaction
) but since the data structure representingsigset_t
is opaque there needs to be a reliable way to initialize it. Local variables in C aren't automatically initialized and you can't just assume sigset_t is an integer type and initialize it to zero because it might be a structure or some other unknown type. Thussigemptyset
andsigfillset
are provided to reliably empty (turn all signals off) or fill (turn all signal on) thesigset_t
representation.Your actual confusion may be what the signal sets inherent in sigset_t are used for and the answer to that is they are used for multiple things. You may want to block multiple signals in one call by passing a set to appropriate call. Or you may want to check all pending signals in one call. In short, it depends, but a set allows you to operate on multiple signals in one shot rather than a sequential series of actions - something which is particularly problematic with signals because they are asynchronous and subject to all kinds of race conditions.