免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 15327 | 回复: 21
打印 上一主题 下一主题

pause()的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-24 15:20 |只看该作者 |倒序浏览
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/time.h>
  4. #include<stdlib.h>
  5. #include<signal.h>
  6. #include<time.h>
  7. #include<string.h>
  8. #include<sys/mman.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include<sys/wait.h>
  12. #include <fcntl.h>


  13. void sig_alrm(int signal)
  14. {
  15.         printf("haha\n");
  16. }

  17. int main(int argc, const char *argv[])
  18. {

  19.         int i;
  20.         pid_t pid;
  21.         pid=fork();
  22.         if(pid==0)
  23.         {
  24.                 struct sigaction act;
  25.                 act.sa_handler=sig_alrm;
  26.                 sigemptyset(&act.sa_mask);
  27.                 act.sa_flags=SA_RESTART;
  28.                 sigaction(SIGALRM,&act,NULL);
  29.                 pause();

  30.         }
  31.         if(pid>0)
  32.         {
  33.                 for(i=1;1;i++)
  34.                 {
  35.                         sleep(1);
  36.                         if((i%5)==0)
  37.                                 kill(pid,SIGALRM);
  38.                         printf("%d\n",i);
  39.                 }
  40.         }

  41.         return 0;
  42. }
复制代码
我这个程序想实现的功能是创建个子进程并且注册信号SIGALRM  他的处理函数是打印一个哈哈 主进程每过1秒钟向子进程发送一个SIGALRM信号 自进程打印haha 我用pause()这个慢速系统调用把子进程挂起 并且act.sa_flags=SA_RESTART 但是为什么pause被信号打断后 没有重新被调用屏幕上只打印了一个haha  请大家帮忙解惑下

论坛徽章:
0
2 [报告]
发表于 2011-04-24 18:34 |只看该作者
我这个程序想实现的功能是创建个子进程并且注册信号SIGALRM  他的处理函数是打印一个哈哈 主进程每过1秒钟向 ...
zhaijinbao 发表于 2011-04-24 15:20



    应该是
  1. while(1) pause();
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-04-25 09:23 |只看该作者
我要用while(1) pause(); 执行就失去这个程序的意义了 我这个程序就是想验证慢速系统调用被中断后能否被再次调用 因为我act.flags=SA_RESTART了 我试验过 把pause()换成 int a; scanf("%d\n",&a); scanf被信号打断后 还能恢复 pause();就不行 这是为什么

论坛徽章:
0
4 [报告]
发表于 2011-04-25 11:49 |只看该作者
man pause
DESCRIPTION
       pause()  causes the calling process (or thread) to sleep until a signal is delivered that either terminates the process or causes the invocation of a signal-catching
       function.

RETURN VALUE
       pause() only returns when a signal was caught and the signal-catching function returned.  In this case pause() returns -1, and errno is set to EINTR.

论坛徽章:
0
5 [报告]
发表于 2011-04-25 11:51 |只看该作者
你可以用read()代替pause()来试验

论坛徽章:
0
6 [报告]
发表于 2011-04-25 13:01 |只看该作者
xuexi

论坛徽章:
0
7 [报告]
发表于 2011-04-25 13:02 |只看该作者
test

论坛徽章:
0
8 [报告]
发表于 2011-04-25 13:04 |只看该作者
tes

论坛徽章:
0
9 [报告]
发表于 2011-04-25 13:05 |只看该作者
vvv

论坛徽章:
0
10 [报告]
发表于 2011-04-25 13:11 |只看该作者
haode 伙
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP