最近在开发与应用端交互的php代码,一些基本的linux下编程变得比较生疏,因此对照《unix环境高级编程》进行ngix内容开发的再次学习。
首先关注的ngx中一些基本的信号处理内容
sigemptyset()用来将参数set信号集初始化并清空。
sigaddset()用来将参数signum
代表的信号加入至参数set 信号集。
int sigprocmask (int __how, __const sigset_t *__restrict __set,sigset_t *__restrict __oset)用于阻塞当前进程中的信号集合
附上一篇较好的关于信号阻塞的分析文章
http://blog.youkuaiyun.com/muge0913/article/details/7334771
int sigsuspend (__const sigset_t *__set)
在标准库的中解释为
This function replaces the process's signal mask with set and then suspends the process until a signal is delivered whose
action is either to terminate the process or invoke a signal handling function. In other words, the program is effectively suspended until one of the signals that is not a member of set arrives.
If the process is woken up by delivery of a signal that invokes a handler function, and the handler function returns, then sigsuspend also returns.
The mask remains set only as long as sigsuspend is waiting. The function sigsuspend always restores the previous signal mask when it returns.
The return value and error conditions are the same as for pause.
理解一下便是将当前的屏蔽信号集变为set中内容,同时系统进程挂起,当接受到一个非屏蔽的信号时,回复到从前的信号mask码。
下面为一个测试的小程序:
void test(int signalnum){
if(signalnum == SIGUSR1){
printf("get the signal usr1");
}
if(signalnum == SIGUSR2){
printf("get the signal usr2");
}
return;
}
int main(int argc,char *argv[]){
sigset_t tmp;
struct sigaction signal_t;
sigemptyset(&signal_t.sa_mask);
signal_t.sa_flags=0;
signal_t.sa_handler=test;
sigaction(SIGUSR1,&signal_t,NULL);
sigaction(SIGUSR2,&signal_t,NULL);
sigemptyset(&tmp);
sigprocmask(0,NULL,&tmp);
sigaddset(&tmp,SIGUSR1);
sigsuspend(&tmp);
exit(0);
}
这个小测试程序的在运行到sigsuspend的时候会挂起,当发送kill -10 pid 的时候,当前程序不会有任何反映,因为10(SIGUSR1) 已经被屏蔽,当发送12(SIGUSR2)时,程序接受到SIGUSR1/SIGUSR2两个信号内容。
附上一篇较好的分析文章:http://blog.chinaunix.net/uid-26885237-id-3206640.html