linux 的sleep()、usleep()、nanosleep()函数的区别
来源:互联网 发布:期权看盘软件 编辑:程序博客网 时间:2024/05/04 04:55
其它时间、日期操作函数:http://blog.csdn.net/u011170660/article/details/38318765
一下三种睡眠时间函数的区别:
sleep()-------以秒为单位
unsigned int sleep(unsigned int seconds);
sleep()非系统调用,sleep()是在库函数中实现的,它是通过alarm()来设定报警时间,使用sigsuspend()将进程挂起在信号SIGALARM上。
sleep()只能精确到秒级上。sleep()会令目前的进程暂停,直到达到参数seconds 所指定的时间,或是被信号所中断。
#include<unistd.h>
return:若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余秒数。
usleep()----以微秒为单位
unsigned int usleep(unsigned int useconds);
除了时间单位为微秒以外,在使用上与sleep()差不多。还有就是实现也是不同的,sleep因为是用alarm实现的,所以时间单位为s ,而usleep的时间单位为us
,那肯定不是由alarm实现的,所以说它们的实现不同,但都是linux用的,而window下不能用,因为都是sleep和usleep都是在unistd.h下定义的。
#include<unistd.h>
return:若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余微秒数。
nanosleep( )---------以纳秒为单位
struct timespec
{
time_t tv_sec; /* 秒seconds */
long tv_nsec; /* 纳秒nanoseconds */
};
int nanosleep(const struct timespec *req, struct timespec *rem);
#include<time.h>
这个函数功能是暂停某个进程直到你规定的时间后恢复,参数req就是你要暂停的时间,其中req->tv_sec是以秒为单位,而tv_nsec以毫微秒为单位(10的-9次方秒)。由于调用nanosleep是是进程进入TASK_INTERRUPTIBLE,这种状态是会相应信号而进入TASK_RUNNING状态的,这就意味着有可能会没有等到你规定的时间就因为其它信号而唤醒,此时函数返回-1,切还剩余的时间会被记录在rem中。
return: 若进程暂停到参数*req所指定的时间,成功则返回0,若有信号中断则返回-1,并且将剩余微秒数记录在*rem中。
注意:unistd.h 是 unix 系统标准头文件,用于系统调用,相当于win32中的windows.h,unistd.h 定义的函数只能用于UNIX环境 中,而不能用于windows。所以sleep 和 usleep 只能用于linux下,而不能用于windows 。
nalosleep 和 其它时间日期操作函数一样都是定义在time.h中的,所以都适用。
- linux 的sleep()、usleep()、nanosleep()函数的区别
- linux 的sleep()、usleep()、nanosleep()函数的区别
- 关于Linux的线程休眠函数sleep/usleep/nanosleep/select
- Linux 高精確的時序(sleep, usleep,nanosleep)
- linux 下面的sleep,usleep,nanosleep 和select比较
- Linux高精确的时序(sleep, usleep,nanosleep)
- linux中sleep()和usleep()函数的使用和区别
- 十九、Linux系统编程-信号(六)三种不同精度的睡眠sleep、usleep、nanosleep
- usleep()和sleep()的区别
- usleep和sleep的区别
- sleep与usleep的区别
- Linux的sleep()和usleep()
- Linux的sleep()和usleep()
- PHP 暂停函数 sleep() 与 usleep() 的区别
- PHP 暂停函数 sleep() 与 usleep() 的区别
- linux的sleep()和usleep()的使用和区别
- linux的sleep()和usleep()的使用和区别
- linux的sleep()和usleep()的使用和区别
- MFC基础类库--设备环境对象类----CDC类
- 写扩展controlBean的方法
- poj 1703 Find them, Catch them(种类并查集)
- 录像机CIF、D1、720P、960P、1080P分辨率及硬盘所占空间
- 字符设备驱动中cdev与inode、file_operations的关系
- linux 的sleep()、usleep()、nanosleep()函数的区别
- node.js 之express web工程目录结构
- MFC打开文件对话框,选择多个文件
- C++ Socket WSAENOBUFS WSAoverlapped
- 不明生物咬死农户18只羊 似狗非狗似豺非豺(图)
- POJ-2253 Frogger
- Linux C----创建静态库
- JAVA中indexOf函数用法和subString()方法
- Spring定时任务的几种实现