最近在开发项目的时候遇到一个问题,当使用 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无法正常休眠的更多相关文章

  1. Linux设备驱动程序 之 休眠

    休眠简介 当一个进程被置入休眠时,它会被标记为一种特殊状态,并从调度器的运行队列中移走:直到某些情况下修改了这个状态,进程才会在任意cpu上调度,即运行该进程:休眠中的进程会被搁置在一边,等待将来的某 ...

  2. (转)Linux 定时关机、休眠命令

    立刻关机:sudo haltsudo init 0 sudo shutdown -h nowsudo shutdown -h 0....定时/延时关机:sudo shutdown -h 19:3019 ...

  3. linux系统的休眠与唤醒简介

    转自:http://blog.csdn.net/haomcu/article/details/7398703 系统挂起(Suspend)是电源管理(APM&ACPI)的一个特性,给用户带来了很 ...

  4. Linux 2.4调度系统分析--转

    http://www.ibm.com/developerworks/cn/linux/kernel/l-k24sch/index.html 杨沙洲 (pubb@163.net)国防科技大学计算机学院 ...

  5. android 休眠唤醒机制分析(二) — early_suspend

    本文转自:http://blog.csdn.net/g_salamander/article/details/7982170 early_suspend是Android休眠流程的第一阶段即浅度休眠,不 ...

  6. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  7. Linux电源管理【转】

    转自:http://www.cnblogs.com/sky-zhang/archive/2012/06/05/2536807.html PM notifier机制: 应用场景: There are s ...

  8. Ubuntu server 禁止显示器休眠

    Linux不让显示器休眠的方法 # setterm -blank # setterm -blank n (n为等待时间)

  9. Linux应用层的定时器Timer使用详解【转】

    转自:http://blog.csdn.net/wwwtovvv/article/details/8601528 版权声明:本文为博主原创文章,未经博主允许不得转载. linux下定时器的使用 -- ...

  10. Linux遗忘命令

    1.查找文件的安装目录,拿nginx来说 find /|grep nginx.conf 2.   a.查询某个端口是否被占用,如8080端口 netstat –apn | grep 8080 b.查看 ...

随机推荐

  1. [转]正确使用SQLCipher来加密Android数据库 - 朝野布告

    参考文档:http://www.tuicool.com/articles/eYNFbuA Android本身自带有不加密的数据库SQLite,如果要保存密码之类的敏感数据在本地的话方法一是使用字段加密 ...

  2. (Gym 100685G) Gadget Hackwrench(LCA在线ST)

    Gadget Hackwrench time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  3. python学习笔记:Day01

    一.python 简介 1.  python是Guido van Rossum在1989年圣诞节期间,为了打发无聊的假期而编写的一个编程语言   2.  pyhton主要应用于数据分析.组件集成.网络 ...

  4. Dynamic CRM 2013学习笔记(二十五)JS调用web service 实现多条记录复制(克隆)功能

    前面介绍过如何克隆一条当前的记录: Dynamic CRM 2013学习笔记(十四)复制/克隆记录 , 主要是通过界面上加一个字段,单击form上的clone 按钮时,改变这个字段的值以触发插件来实现 ...

  5. asp.net froms 移动平台(iphone 微信)无法存储的解决办法。

    更改form的默认设置,让系统不再根据设备来判断是否支持cookie 在站点的配置文件中有关于Form认证的配置,在配置<authentication mode="Forms" ...

  6. A memory leak issue with WPF Command Binding

    Background In our application, we have a screen which hosts several tabs. In each tab, it contains a ...

  7. 曲演杂坛--EXISTS语句

    通常在我写EXISTS语句时,我会写成IF EXISTS(SELECT TOP(1) 1 FROM XXX),也没细细考究过为什么要这么写,只是隐约认为这样写没有啥问题,那今天就深究下吧! 首先准备测 ...

  8. Android中px和dip的区别

    在Android手机的诞生之初,由于Android系统是开源的,一开始便有众多的OEM厂商对Android手机进行深度定制,于是乎Android手机的皮肤和屏幕大小都变得百花齐放,这可苦逼了我们这群开 ...

  9. [游戏模版3] Win32 画笔 画刷 图形

    >_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...

  10. spring mvc ajax 400解决

    The request sent by the client was syntactically incorrect. ajax发起请求时报400错误.请求代码如下: var reportId=($( ...