免费注册 查看新帖 |

Chinaunix

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

[C] 捕获SIGALRM为什么使sleep()或pause()失效了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-08 17:27 |只看该作者 |倒序浏览
10可用积分
我在solaris8+CC和HP-UX+aCC的环境下都测试了,同样的结果

(1)> cat f.cpp
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
extern "C" void f(int s){
  printf("sig handler:%d\n",s);
  alarm(0);
}
int main(void){
  signal(SIGALRM,f);
  alarm(5);
  printf("after alarm\n");
  sleep(1000);
  printf("after sleep\n");//-->注意,根本没有sleep(1000秒),而是立刻打印这句话并退出程序
  return 0;
}
> CC f.cpp && ./a.out
after alarm
sig handler:14
after sleep  

(2)> cat p.cpp
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
extern "C" void sigalrm_fn(int sig)
{
    printf("alarm!\n");
    alarm(2);
    return;
}
int main(void)
{
    signal(SIGALRM, sigalrm_fn);
    alarm(1);
    while(1) pause();//这里的pause循环似乎没有起到作用
}
> CC p.cpp -o b.out && ./b.out
alarm!
Alarm clock
> 程序退出了,并没有每隔一秒钟循环打印一个alarm

这都是为什么呢?

最佳答案

查看完整内容

呵呵,老兄喜欢触犯禁忌啊!下面引用了一堆标准文档,总结起来就是:sleep pause不要和alarm一起使用NAME sleep - suspend execution for an interval of timeSYNOPSIS #include unsigned sleep(unsigned seconds);DESCRIPTION The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or ...

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2009-04-08 17:27 |只看该作者

回复 #1 jeanlove 的帖子

呵呵,老兄喜欢触犯禁忌啊!下面引用了一堆标准文档,总结起来就是:sleep pause不要和alarm一起使用

NAME

    sleep - suspend execution for an interval of time

SYNOPSIS

    #include <unistd.h>

    unsigned sleep(unsigned seconds);

DESCRIPTION

    The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the scheduling of other activity by the system.

    If a SIGALRM signal is generated for the calling process during execution of sleep() and if the SIGALRM signal is being ignored or blocked from delivery, it is unspecified whether sleep() returns when the SIGALRM signal is scheduled. If the signal is being blocked, it is also unspecified whether it remains pending after sleep() returns or it is discarded.

    If a SIGALRM signal is generated for the calling process during execution of sleep(), except as a result of a prior call to alarm(), and if the SIGALRM signal is not being ignored or blocked from delivery, it is unspecified whether that signal has any effect other than causing sleep() to return.

    If a signal-catching function interrupts sleep() and examines or changes either the time a SIGALRM is scheduled to be generated, the action associated with the SIGALRM signal, or whether the SIGALRM signal is blocked from delivery, the results are unspecified.

    If a signal-catching function interrupts sleep() and calls siglongjmp() or longjmp() to restore an environment saved prior to the sleep() call, the action associated with the SIGALRM signal and the time at which a SIGALRM signal is scheduled to be generated are unspecified. It is also unspecified whether the SIGALRM signal is blocked, unless the process' signal mask is restored as part of the environment.

RATIONALE

    There are two general approaches to the implementation of the sleep() function. One is to use the alarm() function to schedule a SIGALRM signal and then suspend the calling thread waiting for that signal. The other is to implement an independent facility. This volume of IEEE Std 1003.1-2001 permits either approach.

    In order to comply with the requirement that no primitive shall change a process attribute unless explicitly described by this volume of IEEE Std 1003.1-2001, an implementation using SIGALRM must carefully take into account any SIGALRM signal scheduled by previous alarm() calls, the action previously established for SIGALRM, and whether SIGALRM was blocked. If a SIGALRM has been scheduled before the sleep() would ordinarily complete, the sleep() must be shortened to that time and a SIGALRM generated (possibly simulated by direct invocation of the signal-catching function) before sleep() returns. If a SIGALRM has been scheduled after the sleep() would ordinarily complete, it must be rescheduled for the same time before sleep() returns. The action and blocking for SIGALRM must be saved and restored.

    Historical implementations often implement the SIGALRM-based version using alarm() and pause(). One such implementation is prone to infinite hangups, as described in pause(). Another such implementation uses the C-language setjmp() and longjmp() functions to avoid that window. That implementation introduces a different problem: when the SIGALRM signal interrupts a signal-catching function installed by the user to catch a different signal, the longjmp() aborts that signal-catching function. An implementation based on sigprocmask(), alarm(), and sigsuspend() can avoid these problems.

论坛徽章:
0
3 [报告]
发表于 2009-04-08 17:36 |只看该作者
注释掉SIGALRM那几行代码,就知道是否失效了?

论坛徽章:
0
4 [报告]
发表于 2009-04-08 17:49 |只看该作者
1 不要同时使用sleep()/alarm()
2 不要使用语义不明确的函数如signal()

评分

参与人数 1可用积分 +2 收起 理由
jeanlove + 2 我很赞同

查看全部评分

论坛徽章:
0
5 [报告]
发表于 2009-04-08 18:04 |只看该作者
(1)sleep 被alarm中断退出,自然不会有那么长的时间。
(2)应该可以实现每隔一秒钟循环打印一个alarm。

评分

参与人数 1可用积分 +2 收起 理由
jeanlove + 2 我很赞同

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP