【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.查看 ...
随机推荐
- linux 禁ping本机方法
linux 禁ping本机方法 禁ping执行:echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 恢复ping 执行:echo 0 > /p ...
- JQuery学习(表单对象属性)---checked
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- HTTP协议状态码详解(HTTP Status Code)
转自:http://www.cnblogs.com/shanyou/archive/2012/05/06/2486134.html 使用ASP.NET/PHP/JSP 或者javascript都会用到 ...
- Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置
上次介绍了<Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示> 以及如何配置自定义审批流的按钮:<Dynamic CRM 2013学习笔记(二十一)自定义 ...
- 【Win10】UAP/UWP/通用 开发之 SplitView
[Some information relates to pre-released product which may be substantially modified before it's co ...
- Agile 是什么?
也许你已经习惯了 “Agile” 这个词汇不断地在你耳边狂轰滥炸,诸如敏捷团队.敏捷UX.敏捷建模.敏捷需求管理.敏捷架构等等.而且,人们还在不断的将 “Agile” 和更多的词汇进行组合,比如,也曾 ...
- 深入理解java虚拟机【垃圾回收算法】
Java虚拟机的内存区域中,程序计数器.虚拟机栈和本地方法栈三个区域是线程私有的,随线程生而生,随线程灭而灭:栈中的栈帧随着方法的进入和退出而进行入栈和出栈操作,每个栈帧中分配多少内存基本上是在类结构 ...
- Python模拟HTTP Post上传文件
使用urllib2模块构造http post数据结构,提交有文件的表单(multipart/form-data),本示例提交的post表单带有两个参数及一张图片,代码如下: #buld post bo ...
- AngularJS快速入门指南07:Http对象
$http是AngularJS提供的一个服务,用来从远程服务器读取数据. 提供数据 下面的数据由Web服务器提供: { "records": [ { "Name" ...
- mysql where执行顺序
where执行顺序是从左往右执行的,在数据量小的时候不用考虑,但数据量多的时候要考虑条件的先后顺序,此时应遵守一个原则:排除越多的条件放在第一个. 在用MySQL查询数据库的时候,连接了很多个过滤条件 ...