Signal handling and sigemptyset()

泄露秘密 提交于 2019-12-02 19:12:26
Devolus

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!