|
- #include<signal.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<unistd.h>
- static void child_sigfun(int signo){
- if( signo == SIGALRM){
- puts("catch SIGALRM\n");
- }
- }
- static void parent_sigfun(int signo){
- if(signo == SIGUSR1){
- puts("catch SIGUSR1");
- }
- }
- int main(){
- pid_t pid,ppid;
- sigset_t sigs,psigs;
- struct sigaction psa_new;
- struct sigaction psa_old;
- struct sigaction sa_new;
- struct sigaction sa_old;
- psa_new.sa_handler = parent_sigfun;
- sigemptyset(&psa_new.sa_mask);
- sigaddset(&psa_new.sa_mask,SIGALRM);
- psa_new.sa_flags = 0;
- sigaction(SIGUSR1,&psa_new,&psa_old);
- sigfillset(&sigs);
- sigfillset(&psigs);
- sigdelset(&psigs,SIGUSR1);
- sigdelset(&sigs,SIGALRM);
- sa_new.sa_handler = child_sigfun;
- sigemptyset(&sa_new.sa_mask);
- sigaddset(&sa_new.sa_mask,SIGUSR1);
- sa_new.sa_flags = 0;
- sigaction(SIGALRM,&sa_new,&sa_old);
- if((pid = fork()) == 0){
- puts("child process\n");
- alarm(3);
- sigsuspend(&sigs);
- kill(getppid(),SIGUSR1);
- }else{
- puts("parent process\n");
- sigsuspend(&psigs);
- }
- return 0;
- }
复制代码
[root@localhost linux24]# gcc signalset.c
[root@localhost linux24]# ./a.out
parent process
child process
catch SIGALRM
Segmentation fault
找不出原因,请指教,谢谢! |
|