sleep function error ("Advanced Programming in the UNIX Environment" Third Edition No.374)
测试证明代码:
#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)的更多相关文章
- Atlassian JavaScript load error(配置Atlassian的时候报无法加载script文件)
等我弄好的时候,发现报错的图已经成为历史了,所以就没有截图了. Atlassian JavaScript load error We tried to load scripts but somethi ...
- javaScript设计模式之面向对象编程(object-oriented programming,OOP)(一)
面试的时候,总会被问到,你对javascript面向对象的理解? 面向对象编程(object-oriented programming,OOP)是一种程序设计范型.它讲对象作为程序的设计基本单元,讲程 ...
- Sth about 函数式编程(Functional Programming)
今天开会提到了函数式编程,针对不同类型的百年城方式,查阅了一部分资料,展示如下: 编程语言一直到近代,从汇编到C到Java,都是站在计算机的角度,考虑CPU的运行模式和运行效率,以求通过设计一个高效的 ...
- 编程范式 --- 函数式编程(Funtional Programming,简称FP)
函数式编程(Funtional Programming,简称FP)是一种编程范式,也就是如何编写程序的方法论 主要思想:把计算过程尽量分解成一系列可复用函数的调用 主要特征:函数是"第一等公 ...
- 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)) 修改: # ...
- Sql server2012连接Sql server 2008时出现的问题:已成功与服务器建立连接,但在登陆过程中发生错误。(provider:SSL Provider,error:0-接收到的消息异常,或格式不正确。)
以前连接是正常的,就这两天连不上了.(没有耐心的直接看末尾解决办法) 错误消息如下: 1.尝试读取或写入受保护的内存.这通常指示其他内存已损坏.(System.Data) 2.已成功与服务器建立连接, ...
- function adapter(函数适配器)和迭代器适配器
所谓function adapter(函数适配器)是指能够将不同的函数对象(或是和某值或某寻常函数)结合起来的东西,它自身也是个函数对象. 迭代器适配器 运用STL中的迭代器适配器,可以使得算法能够 ...
- ALV详解:Function ALV(二)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ALV详解:Function ALV(一)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
随机推荐
- python 面向对象(类的成员,属性,绑定和非绑定,)
面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...
- oracle完全之dbf文件出现问题, ORA-01219
alter database datafile '/data/app/oradata/ora237/users01.dbf' offline drop; 强制删除该故障文件
- makefile——小试牛刀
//a.h,包含头文件stdio.h,并且定义一个函数print #include<stdio.h> void print(); //b.c,包含头文件a.h,然后就可以写print函数的 ...
- Centos7部署ntp服务器同步时间以及直接将本地时间同步为北京时间
一.查看配置 查看时区列表: timedatectl list-timezones|grep Asia 查看当前时间: date 查看当前设置: [root@localhost ~]# timedat ...
- cos migration工具webhook推送
上一篇讲了腾讯云同步工具的使用,这篇主要是补充如何将同步结果主动消息通知. 因为cos migration 工具是java语言,并在github开源的,所以可以直接修改源码,添加webhook推送代码 ...
- svn already lock解决方法
svn在pull的时候,出现svn already lock 错误.只需要在Cleanup里面勾选Break locks
- file命令详解
Linux file命令 Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型 用法: file [-bchikLNnprsvz0] [--apple] [--mim ...
- Feign 请求拦截器和日志
Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,自定请求拦截器需要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参 ...
- MySQL数据库分区操作【RANGE】
客服平台,线上查询存在性能问题,为了解决或者说是缓解这个问题,除了加必要的索引,另外就是将表进行分区. 这里主要是针对既有的表进行分区,采用的是alter table xxx的方式,当然,也可以采用c ...
- supervisor的安装部署及集群管理
supervisor的安装部署及集群管理 supervisor官网:http://www.supervisord.org/ 参考链接: http://blog.csdn.net/xyang81/art ...