sigaction-sigqueue

2019-07-14 08:43发布

#include #include #include #include /********************************************************************* PCB: block----------pending 0 1 1 0 sigprocmask sigpending if signal is blocked(sigprocmask),the bit is 1,we can read the bit to use sigpending *********************************************************************/ #if 0 struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); }; #endif #if 0 union sigval { int sival_int; void *sival_ptr; }; #endif void handler(int num,siginfo_t *s_t,void *p) { int receive_data = 0; if(num == SIGINT) { receive_data = s_t->si_int; printf("receive sig num is %d,data is %d ",num,receive_data); }else if(num == SIGRTMIN){ receive_data = s_t->si_int; printf("receive sig num is %d,data is %d ",num,receive_data); }else if(num == SIGUSR1) { sigset_t set;//for set the bit of block sigemptyset(&set); sigaddset(&set,SIGINT); sigaddset(&set,SIGRTMIN); sigprocmask(SIG_UNBLOCK,&set,NULL); printf("unblock of sigint and signalrtmin...... "); } } int main() { sigset_t set;//for set the bit of block struct sigaction my_set; my_set.sa_sigaction = handler; my_set.sa_flags=SA_SIGINFO; sigaction(SIGINT,&my_set ,NULL); sigaction(SIGRTMIN,&my_set,NULL); sigaction(SIGUSR1,&my_set,NULL); sigemptyset(&set); sigaddset(&set,SIGINT); sigaddset(&set,SIGRTMIN); sigprocmask(SIG_BLOCK,&set,NULL); pid_t pid = fork(); if(pid == 0) { int i = 0; union sigval test; test.sival_int = 201; for(i = 0; i <3 ;i++) { printf("send sigint.... "); sigqueue(getppid(),SIGINT,test); test.sival_int++; } test.sival_int = 300; for(i = 0; i <3 ;i++) { printf("send sigrtmin..... "); sigqueue(getppid(),SIGRTMIN,test); test.sival_int++; } kill(getppid(),SIGUSR1); }else if(pid > 0){ while(1) { sleep(1); } } } send sigint.... send sigint.... send sigint.... send sigrtmin..... send sigrtmin..... send sigrtmin..... receive sig num is 34,data is 300 receive sig num is 34,data is 301 receive sig num is 34,data is 302 receive sig num is 2,data is 201 unblock of sigint and signalrtmin...... as:
1.可靠信号与不可靠信号同时发了3次,可靠信号全部抵达,不可靠信号抵达1次。
2.sigprocmask阻塞后,解除阻塞信号仍然会抵达。
3.sigqueue和sigaction可以传送数据。
4.block为1,pending就是0,表示未达状态。当block为unblock后,sending为0,信号可以抵达。
5.不解之处:SIGINT已经设置为unblock,但是ctrl+c,仍然无法终止进程。