alarm之后调用sleep会这样

#include "apue.h"
#include <setjmp.h>
#include <time.h>
static void     sig_usr1(int), sig_alrm(int);
static sigjmp_buf       jmpbuf;
static volatile sig_atomic_t    canjump;
int
main(void)
{
        if (signal(SIGUSR1, sig_usr1) == SIG_ERR)
                err_sys("signal(SIGUSR1) error");
        if (signal(SIGALRM, sig_alrm) == SIG_ERR)
                err_sys("signal(SIGALRM) error");
        pr_mask("starting main: ");             /* {Prog prmask} */
        if (sigsetjmp(jmpbuf, 1)) {
                pr_mask("ending main: ");
                exit(0);
        }
        canjump = 1;    /* now sigsetjmp() is OK */
        for ( ; ; )
                pause();
}

static void
sig_usr1(int signo)
{
        time_t  starttime;

        if (canjump == 0)
                return;         /* unexpected signal, ignore */

        pr_mask("starting sig_usr1: ");

alarm(3);  

sleep(5);
        pr_mask("finishing sig_usr1: ");
        canjump = 0;
        siglongjmp(jmpbuf, 1);  /* jump back to main, don't return */

}

static void
sig_alrm(int signo)
{
        pr_mask("in sig_alrm: ");
}

按说在sleep之后应该执行sig_alrm函数的,可是为什么没执行呢?

 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.
这是答案,唉,折腾了一晚上啊

转载于:https://my.oschina.net/u/1587315/blog/290044

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值