测试证明代码:

#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "apue.h" static void sig_alrm(int signo)
{
/* nothing to do, just returning wakes up sigsuspend */
} static void sig_usr1(int signo)
{ } //static void sig_alrm_test(int signo)
//{
// err_msg("left an alrm");
//} unsigned int sleep(unsigned int seconds)
{
struct sigaction newact, oldact;
sigset_t newmask, oldmask, suspmask;
unsigned int unslept; timespec ts; // ++
ts.tv_sec = ; // ++
ts.tv_nsec = ; // ++ /* set our handler, save previous information */
newact.sa_handler = sig_alrm;
sigemptyset(&newact.sa_mask);
newact.sa_flags = ;
sigaction(SIGALRM, &newact, &oldact); /* block SIGALRM and save current signal mask */
sigemptyset(&newmask);
sigaddset(&newmask, SIGALRM);
sigprocmask(SIG_BLOCK, &newmask, &oldmask); alarm(seconds);
suspmask = oldmask; /* make sure SIGALRM isn't block */
sigdelset(&suspmask, SIGALRM); /* wait for any signal to be caught */
sigsuspend(&suspmask); nanosleep(&ts, NULL); // ++ /* some signal has been caught, SIGALRM is now blocked */
unslept = alarm(); /* reset previous action */
sigaction(SIGALRM, &oldact, NULL); /* reset signal mask, which unblocks SIGALRM */
sigprocmask(SIG_SETMASK, &oldmask, NULL);
return unslept;
} int main(int argc, char *argv[])
{
struct sigaction useract;
struct sigaction alrmact; useract.sa_handler = sig_usr1;
sigemptyset(&useract.sa_mask);
useract.sa_flags = ;
if (sigaction(SIGUSR1, &useract, NULL) < )
err_sys("SIG SUR1 error"); // alrmact.sa_handler = sig_alrm_test;
// sigemptyset(&alrmact.sa_mask);
// alrmact.sa_flags = 0;
// if (sigaction(SIGALRM, &alrmact, NULL) < 0)
// err_sys("SIG ALRM error"); sleep(); return ;
}

操作说明,这里我给出我和作者交流时的说明,所以用的是英文,不过我英文水平有限,希望谅解:

I should give you my manipulation. You should send a SIGUSR1 to the process in less than 20 seconds. So that when alarm(0) tries to cancel the alarm, it was not expired. You see, I didn't send the process SIGALARM, but the SIGALARM handler was fired.

下面是运行时候的结果(为了让作者可以看清楚,使用的是英文):

下面是作者的回复(中间有一些曲折,给出最后的部分回复内容):

Yes, there is a very small window where if the sigsuspend() is interrupted by a different signal and the SIGALRM is posted before calling alarm(0) to cancel it, that the process will be terminated if the default disposition of SIGALRM is in effect.  You have artificially widened that window by adding the call to nanosleep().

最新文章

  1. 【swift学习笔记】五.使用枚举优雅的管理Segue
  2. python3安装lxml(windows)
  3. [C/C++] DebugBreak
  4. [转]Visual Studio技巧之打造拥有自己标识的代码模板
  5. Spark1.0源码编译
  6. myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题
  7. 微信企业支付--遇到不明确结果的err_code:SYSTEMERROR,NOT_FOUND
  8. poj 1564 Sum It Up (DFS+ 去重+排序)
  9. sudo source /etc/profile 提示找不到source命令
  10. C# 几十万级数据导出Excel,及Excel各种操作
  11. 怎样用HTML5 Canvas制作一个简单的游戏
  12. android异步Http框架
  13. seo步骤
  14. MarsEdit快速插入源代码
  15. Golang源码探索(二) 协程的实现原理
  16. Activity切换动画---点击哪里从哪放大
  17. Struts 2 之拦截器
  18. jQuery实现全选、反选和不选功能
  19. 清明培训 清北学堂 DAY2
  20. 现代IM系统中消息推送和存储架构的实现

热门文章

  1. Vs code 设置
  2. Layer 弹出页面 在点击保存关闭弹出层
  3. 2017-2018-2 20165313实验二《Java面向对象程序设计》
  4. powerdesigner基本使用
  5. ionic后台返回的数据是html模板的时候,解析html文件的方法:
  6. centos 7.0 lnmp成功安装过程(很乱)
  7. 05C++引用
  8. HTTP请求属性说明
  9. Hi3516CV300 sample -&gt; region
  10. What is the difference between concurrency, parallelism and asynchronous methods?