信号屏蔽字

linux的进程有一个信号屏蔽字,如果某个信号在信号屏蔽字中被置位,则当产生该信号时,内核并不会把该信号递送给进程,

这称为信号的未决。当该信号从信号屏蔽字中移除时,内核会把未决信号递送给进程,进程执行对应的信号处理函数。

//获取或设置进程的信号屏蔽字,把信号屏蔽字的旧值存到oset中
/* Get and/or change the set of blocked signals.  */
extern int sigprocmask (int __how, const sigset_t *__restrict __set,
            sigset_t *__restrict __oset) __THROW;

//示例
sigset_t newmask, oldmask;
sigemptyset(&newmask);
sigaddset(&newmask, SIGINT);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);

//临时修改进程的信号屏蔽字,从信号处理函数返回后,恢复信号屏蔽字
/* Change the set of blocked signals to SET, wait until a signal arrives,
and restore the set of blocked signals. This function is a cancellation point
and therefore not marked with __THROW. */
extern int sigsuspend (const sigset_t *__set) __nonnull ((1));

//示例
sigset_t waitmask;
sigemptyset(&waitmask);
sigaddset(&waitmask, SIGUSR1);
//进程被阻塞,除SIGUSR1以外的信号,会递送到进程,执行信号处理函数,然后从阻塞返回。
//即信号SIGUSR1会被阻塞,其他信号可以唤醒进程
sigsuspend(&waitmask);

在linux中,通过 kill 命令给进程发送信号:

如:kill -SIGINT pid, kill -SIGUSR1 pid,kill -SIGKILL pid

原文地址:https://www.cnblogs.com/allenwas3/p/8024022.html