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,并以超链接形式标明文章原始出处,否则将 ...
随机推荐
- 组合数的求法 (n<=1e8 可以过来看)
C(n,m) =n!/(m!* (n-m)! ); o(n) 求 1-m的逆元 o(n) 求 n的阶乘 代码实现 https://www.cnblogs.com/linyujun/p/5194189 ...
- 【BZOJ1067】【SCOI2007】降雨量
新人求助,降雨量那题,本机AC提交AC 原题: 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例 ...
- xenserver使用快照创建虚拟机,提示eth0 has different mac错误
这个报错的意思就是说mac地址错误 我们对比后可以发现,用快照创建的虚拟机和原虚拟机的eth0那个配置文件的 mac地址是一样的,因为mac地址具有唯一性,所以就报这个错,无法配置ip上网 解决方法很 ...
- Using Elixir Dynamic Supervisors
转自: https://blog.smartlogic.io/elixir-dynamic-supervisors/ I have been working on my side project Gr ...
- DevExpress DateEdit控件选择精确到秒
先看看下面的效果图: 设置以下属性 dateEdit1.Properties.VistaDisplayMode = DevExpress.Utils.DefaultBoolean.True; date ...
- Tensorflow 笔记
TensorFlow笔记-08-过拟合,正则化,matplotlib 区分红蓝点 TensorFlow笔记-07-神经网络优化-学习率,滑动平均 TensorFlow笔记-06-神经网络优化-损失函数 ...
- 混合pyqt和qtcreator
目的:测试能否用qtcreator编辑ui资源,而用python编写代码逻辑 import sysfrom PyQt4 import QtGui, uic class MyWindow(QtGui.Q ...
- MyBatis 学习资料
MyBatis 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-typ ...
- docker for mac 安装 kubernetes、kubernetes dashboard
安装参考地址(按照此文档,安装成功):https://yq.aliyun.com/articles/508460 官方说明:https://kubernetes.io/docs/tasks/acces ...
- DefinePlugin插件
这个插件允许你创建全局常量用于编译时解析.如果设置mode:"production",webpack默认会设置"process.env.NODE_ENV": J ...