测试证明代码:

#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().

sleep function error ("Advanced Programming in the UNIX Environment" Third Edition No.374)的更多相关文章

  1. Atlassian JavaScript load error(配置Atlassian的时候报无法加载script文件)

    等我弄好的时候,发现报错的图已经成为历史了,所以就没有截图了. Atlassian JavaScript load error We tried to load scripts but somethi ...

  2. javaScript设计模式之面向对象编程(object-oriented programming,OOP)(一)

    面试的时候,总会被问到,你对javascript面向对象的理解? 面向对象编程(object-oriented programming,OOP)是一种程序设计范型.它讲对象作为程序的设计基本单元,讲程 ...

  3. Sth about 函数式编程(Functional Programming)

    今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...

  4. 编程范式 --- 函数式编程(Funtional Programming,简称FP)

    函数式编程(Funtional Programming,简称FP)是一种编程范式,也就是如何编写程序的方法论 主要思想:把计算过程尽量分解成一系列可复用函数的调用 主要特征:函数是"第一等公 ...

  5. mysql 链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES))

    mysql链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)) 修改: # ...

  6. Sql server2012连接Sql server 2008时出现的问题:已成功与服务器建立连接,但在登陆过程中发生错误。(provider:SSL Provider,error:0-接收到的消息异常,或格式不正确。)

    以前连接是正常的,就这两天连不上了.(没有耐心的直接看末尾解决办法) 错误消息如下: 1.尝试读取或写入受保护的内存.这通常指示其他内存已损坏.(System.Data) 2.已成功与服务器建立连接, ...

  7. function adapter(函数适配器)和迭代器适配器

    所谓function adapter(函数适配器)是指能够将不同的函数对象(或是和某值或某寻常函数)结合起来的东西,它自身也是个函数对象. 迭代器适配器  运用STL中的迭代器适配器,可以使得算法能够 ...

  8. ALV详解:Function ALV(二)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. ALV详解:Function ALV(一)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

随机推荐

  1. C++数组排序

    #include<stdio.h> #include<stdlib.h> #include<windows.h> #define SIZE 5 //数组中元素的数量 ...

  2. Windows server 2008 R2 安装指引

    1.虚拟机实验安装win server 2008 R2 企业版, 安装环境: 虚拟机版本:VM 14 本地系统:win 7 64 位 专业版 打开VM14,新建虚拟机,选择自定义高级(此处也可以选择典 ...

  3. Ubuntu下一个好用的终端

    在终端下输入: sudo apt-get install terminator 快捷键: shift+ctrl+e          在当前窗口右侧新开一个窗口 shift+ctrl+w        ...

  4. 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.4 约束(Pattern的一部分)

    4.4.3 约束(Pattern的一部分) 前面我们已经介绍了条件约束在Pattern中位置了,那么什么是条件约束呢?简单来说就是一个返回true或者false的表达式,比如下面的5小于6,就是一个约 ...

  5. MySQL Innodb Engine --独立表空间参数(innodb_file_per_table)

    MySQL中参数innodb_file_per_table决定将表存放于ibdata*的共享表空间还是独立的.ibd文件的独立表空间. ================================ ...

  6. classLoader卸载与jvm热部署

    以下的相关介绍都是在未使用dcevm的情况 classLoader的卸载机制 jvm中没有提供class及classloader的unload方法.那热部署及osgi中是通过什么机制来实现的呢?实现思 ...

  7. day39 css

    一.css的引入方式 在HTML中引入css方式总共有三种: 行内样式 内接样式 外接样式 3.1 链接式 3.1 导入式 css介绍 现在的互联网前端分三层: HTML:超文本标记语言.从语义的角度 ...

  8. MySQL5.6新特性之Multi-Range Read

    一 介绍    MySQL 5.6版本提供了很多性能优化的特性,其中之一就是 Multi-Range Read 多范围读(MRR) , 它的作用针对基于辅助/第二索引的查询,减少随机IO,并且将随机I ...

  9. JavaScript 隐式类型转换之:加号+

    加号+,有些情况下,它是算术加号,有些情况下,是字符串连接符号 如果字符串和数字相加,JavaScript会自动把数字转换成字符,不管数字在前还是字符串在前 "2" + 3; // ...

  10. ASP.NET AJAX入门系列(3):使用ScriptManagerProxy控件

    在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和Content-Page中需要引入不同的脚本时, ...