- 论坛徽章:
- 0
|
- #include<stdio.h>
- #include<unistd.h>
- #include<sys/time.h>
- #include<stdlib.h>
- #include<signal.h>
- #include<time.h>
- #include<string.h>
- #include<sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include<sys/wait.h>
- #include <fcntl.h>
- void sig_alrm(int signal)
- {
- printf("haha\n");
- }
- int main(int argc, const char *argv[])
- {
- int i;
- pid_t pid;
- pid=fork();
- if(pid==0)
- {
- struct sigaction act;
- act.sa_handler=sig_alrm;
- sigemptyset(&act.sa_mask);
- act.sa_flags=SA_RESTART;
- sigaction(SIGALRM,&act,NULL);
- pause();
- }
- if(pid>0)
- {
- for(i=1;1;i++)
- {
- sleep(1);
- if((i%5)==0)
- kill(pid,SIGALRM);
- printf("%d\n",i);
- }
- }
- return 0;
- }
复制代码 我这个程序想实现的功能是创建个子进程并且注册信号SIGALRM 他的处理函数是打印一个哈哈 主进程每过1秒钟向子进程发送一个SIGALRM信号 自进程打印haha 我用pause()这个慢速系统调用把子进程挂起 并且act.sa_flags=SA_RESTART 但是为什么pause被信号打断后 没有重新被调用屏幕上只打印了一个haha 请大家帮忙解惑下 |
|