Signal handling and sigemptyset()

后端 未结 3 1962
轮回少年
轮回少年 2020-12-24 07:28

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. Fro

相关标签:
3条回答
  • 2020-12-24 07:57

    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 representing sigset_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. Thus sigemptyset and sigfillset are provided to reliably empty (turn all signals off) or fill (turn all signal on) the sigset_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.

    0 讨论(0)
  • 2020-12-24 08:06

    sigemptyset simply initializes the signalmask 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 a memset(0) but you don't have to know the details of the implementation. So if the sa_mask member is changed you don't need to adjust your code because it will be taken care of by sigemptyset.

    0 讨论(0)
  • 2020-12-24 08:10

    The name says it all. sigemptyset() empties (a variable describing) a set of signals.

    That is

    sigset_t ss;
    sigemptyset(&ss);
    

    initialises a variable of type sigset_t (ss here) to "contain" no signals.

    From man sigemptyset (Linux):

    sigemptyset() initializes the signal set given by set to empty, with all signals excluded from the set.

    [...]

    Objects of type sigset_t must be initialized by a call to either sigemptyset() or sigfillset() before being passed to the functions sigaddset(), sigdelset() and sigismember() [...]

    From the POSIX specs:

    int sigemptyset(sigset_t *set); 
    

    [...]

    The sigemptyset() function initializes the signal set pointed to by set, such that all signals defined in POSIX.1-2008 are excluded.

    0 讨论(0)
提交回复
热议问题