【Linux】——sleep无法正常休眠
最近在开发项目的时候遇到一个问题,当使用 sleep(2) 的时候,程序居然没有按照指定的时间去休眠,但是连续执行两次 sleep(2) 的时候,程序可以正常的休眠 2 秒。真是见鬼了。最后查看了以下 sleep 函数的 man 手册,找到了原因。 man 手册如下:
SYNOPSIS
#include <unistd.h>
unsigned int
sleep(unsigned int seconds);
DESCRIPTION
The sleep() function suspends execution of the calling process until
either seconds seconds have elapsed or a signal is delivered to the
process and its action is to invoke a signal-catching function or to ter-
minate the process. System activity may lengthen the sleep by an inde-
terminate amount.
RETURN VALUES
If the sleep() function returns because the requested time has elapsed,
the value returned will be zero. If the sleep() function returns due to
the delivery of a signal, the value returned will be the unslept amount
(the requested time minus the time actually slept) in seconds.
也就是说在执行第一个 sleep 的时候,进程正准备休眠的时候,被一个信号唤醒了,再执行第二个 sleep 的时候,没有了信号,进程可以正常的休眠。为了避免这种情况的发生,我们可以根据 sleep 的返回值,重新定义一个函数来让 sleep 真正的休眠指定的秒数。代码如下:
static int sleep_with_restart(int second) {
int left = second;
while (left > ) {
left = sleep(left);
}
return ;
}
【Linux】——sleep无法正常休眠的更多相关文章
- Linux设备驱动程序 之 休眠
休眠简介 当一个进程被置入休眠时,它会被标记为一种特殊状态,并从调度器的运行队列中移走:直到某些情况下修改了这个状态,进程才会在任意cpu上调度,即运行该进程:休眠中的进程会被搁置在一边,等待将来的某 ...
- (转)Linux 定时关机、休眠命令
立刻关机:sudo haltsudo init 0 sudo shutdown -h nowsudo shutdown -h 0....定时/延时关机:sudo shutdown -h 19:3019 ...
- linux系统的休眠与唤醒简介
转自:http://blog.csdn.net/haomcu/article/details/7398703 系统挂起(Suspend)是电源管理(APM&ACPI)的一个特性,给用户带来了很 ...
- Linux 2.4调度系统分析--转
http://www.ibm.com/developerworks/cn/linux/kernel/l-k24sch/index.html 杨沙洲 (pubb@163.net)国防科技大学计算机学院 ...
- android 休眠唤醒机制分析(二) — early_suspend
本文转自:http://blog.csdn.net/g_salamander/article/details/7982170 early_suspend是Android休眠流程的第一阶段即浅度休眠,不 ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- Linux电源管理【转】
转自:http://www.cnblogs.com/sky-zhang/archive/2012/06/05/2536807.html PM notifier机制: 应用场景: There are s ...
- Ubuntu server 禁止显示器休眠
Linux不让显示器休眠的方法 # setterm -blank # setterm -blank n (n为等待时间)
- Linux应用层的定时器Timer使用详解【转】
转自:http://blog.csdn.net/wwwtovvv/article/details/8601528 版权声明:本文为博主原创文章,未经博主允许不得转载. linux下定时器的使用 -- ...
- Linux遗忘命令
1.查找文件的安装目录,拿nginx来说 find /|grep nginx.conf 2. a.查询某个端口是否被占用,如8080端口 netstat –apn | grep 8080 b.查看 ...
随机推荐
- IHttpActionResult – new way of creating responses in ASP.NET Web API 2
先收藏这篇文章. http://www.strathweb.com/2013/06/ihttpactionresult-new-way-of-creating-responses-in-asp-net ...
- mysql(或者mariadb)连接工具HeidiSQL
Some infos around HeidiSQL Project website: http://www.heidisql.com/Google Code: http://code.google. ...
- 让ASP.NET Web API支持POST纯文本格式(text/plain)的数据
今天在web api中遇到了这样一个问题,虽然api的参数类型是string,但只能接收post body中json格式的string,不能接收原始string. web api是这样定义的: pub ...
- .NET跨平台:再见dnx,你好dotnet cli
昨天在github上dnx的一个issue中看到这样一段话: we're retiring dnx/dnu/dnvm toolchain and will move to dotnet CLI in ...
- PDB调试Python程序
pdb是python内置的调试工具, 它可以在终端中调试Python程序, 这允许pdb在很多无法安装IDE的服务器上使用. 虽然远程调试使用广泛, 但在必要的时候(比如难以在本地搭建运行环境)pdb ...
- Twitter API升级至1.1
Twitter API 1.1是至今最大的一次升级,从3月份提出,到6月11日1.0版本已经全面停止调用.关于1.1版本升级特性可访问: https://dev.twitter.com/docs/ap ...
- [matlab] 矩阵操作
>_<:矩阵构造 1.简单矩阵构造 最简单的方法是采用矩阵构造符“[]”.构造1´n矩阵(行向量)时,可以将各元素依次放入矩阵构造符[]内,并且以空格或者逗号分隔:构造m´n矩阵时,每行如 ...
- 关于消除if-else的简单总结
if-else是计算机语言中基本的分支语句,虽然很简单,但可能会出现滥用的情况,如图: 这种标记判断,嵌套成这样(其实没有必要嵌套),会让别人觉得作者水平很低. 可以看出,这些if仅仅是在判断一个变量 ...
- 使用TabBarController(代码实现)
step01:使用Xcode创建一个项目 step02:填写项目必要信息 step03:检查文件结构树是否正确 step04:创建一些类,这些类将会在后面用到!(选择Swift File) step0 ...
- 关于eclipse android 在manifest改app应用包名注意事项
在我刚学android 时候,然后立即就做项目.那时连eclipse 使用都不是很熟练.很多功能都不知道.新手如果这时去改app应用的包名,没有改好会变成所有控件在R文件里面id都找不到. 先上两张图 ...