【Linux】延时函数sleep、usleep、nanosleep、select、pselect的比较

2024-05-08 05:18

本文主要是介绍【Linux】延时函数sleep、usleep、nanosleep、select、pselect的比较,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、简介

sleep()-------以秒为单位
#include<unistd.h>
unsigned int sleep(unsigned int seconds);
return:若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余秒数。
在linux中,sleep是通过nanosleep实现的。在一些其他系统中(例如POSIX.1),它是通过alarm()来实现的。

usleep()----以微秒为单位
#include<unistd.h>
unsigned int usleep(unsigned int useconds);
return:若进程暂停到参数seconds 所指定的时间,成功则返回0,若有信号中断则返回剩余微秒数。

nanosleep( )---------以纳秒为单位
#include<time.h>
struct timespec
{
time_t tv_sec; /* 秒seconds /
long tv_nsec; /
纳秒nanoseconds */
};
int nanosleep(const struct timespec req, struct timespec rem);
return: 若进程暂停到参数
req所指定的时间,成功则返回0,若有信号中断则返回-1,并且将剩余微秒数记录在
rem中。
req->tv_sec是以秒为单位,而tv_nsec以毫微秒为单位(10的-9次方秒)。
由于调用nanosleep是是进程进入TASK_INTERRUPTIBLE,这种状态是会相应信号而进入TASK_RUNNING状态的。

2、注意

使用这些函数时一定要注意判断返回值。有时候会出现sleep函数被系统中断的情况,导致结果不符合预期。
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) {}

3、精确度对比

低精度情况(100000us及以上):usleep和nanosleep表现差不多。select和pselect表现较差。
高精度情况(100000us及以上):四者表现差不多。
fuction time(usec) realtime reduce

----------------------------------------------------usleep         500000     500091         91nanosleep      500000     500089         89select         500000     500540        540pselect        500000     500549        549
--------------------------------usleep         100000     100078         78nanosleep      100000     100110        110select         100000     100157        157pselect        100000     100149        149
--------------------------------usleep          50000      50091         91nanosleep       50000      50107        107select          50000      50111        111pselect         50000      50084         84
--------------------------------usleep          10000      10086         86nanosleep       10000      10091         91select          10000      10089         89pselect         10000      10088         88
--------------------------------usleep           1000       1089         89nanosleep        1000       1065         65select           1000       1065         65pselect          1000       1066         66
--------------------------------usleep            900        969         69nanosleep         900        974         74select            900        970         70pselect           900        980         80
--------------------------------usleep            500        569         69nanosleep         500        565         65select            500        569         69pselect           500        569         69
--------------------------------usleep            100        166         66nanosleep         100        165         65select            100        163         63pselect           100        163         63
--------------------------------usleep             10         73         63nanosleep          10         76         66select             10         73         63pselect            10         78         68
--------------------------------usleep              1         64         63nanosleep           1         66         65select              1         65         64pselect             1         63         62
--------------------------------
4、测试代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<sys/time.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/select.h>int main(int argc, char **argv)
{unsigned int nTimeTestSec = 0;unsigned int nTimeTest = 0;struct timeval tvBegin;struct timeval tvNow;int ret = 0;unsigned int nDelay = 0;struct timeval tv;int fd = 1;int i = 0;struct timespec req;unsigned int delay[20] = {500000, 100000, 50000, 10000, 1000, 900, 500, 100, 10, 1, 0};int nReduce = 0; //误差fprintf(stderr, "%19s%12s%12s%12s\n", "fuction", "time(usec)", "realtime", "reduce");fprintf(stderr, "----------------------------------------------------\n");for (i = 0; i < 20; i++){if (delay[i] <= 0)break;nDelay = delay[i];//test sleepgettimeofday(&tvBegin, NULL);ret = usleep(nDelay);if(ret == -1){fprintf(stderr, "usleep error, errno=%d [%s]\n", errno, strerror(errno));}gettimeofday(&tvNow, NULL);nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;nReduce = nTimeTest - nDelay;fprintf (stderr, "\t usleep       %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);//test nanosleepreq.tv_sec = nDelay/1000000;req.tv_nsec = (nDelay%1000000) * 1000;gettimeofday(&tvBegin, NULL);ret = nanosleep(&req, NULL);if (-1 == ret){fprintf (stderr, "\t nanousleep   %8u   not support\n", nDelay);}gettimeofday(&tvNow, NULL);nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;nReduce = nTimeTest - nDelay;fprintf (stderr, "\t nanosleep    %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);//test selecttv.tv_sec = 0;tv.tv_usec = nDelay;gettimeofday(&tvBegin, NULL);ret = select(0, NULL, NULL, NULL, &tv);if (-1 == ret){fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));}gettimeofday(&tvNow, NULL);nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;nReduce = nTimeTest - nDelay;fprintf (stderr, "\t select       %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);//pselcetreq.tv_sec = nDelay/1000000;req.tv_nsec = (nDelay%1000000) * 1000;gettimeofday(&tvBegin, NULL);ret = pselect(0, NULL, NULL, NULL, &req, NULL);if (-1 == ret){fprintf(stderr, "select error. errno = %d [%s]\n", errno, strerror(errno));}gettimeofday(&tvNow, NULL);nTimeTest = (tvNow.tv_sec - tvBegin.tv_sec) * 1000000 + tvNow.tv_usec - tvBegin.tv_usec;nReduce = nTimeTest - nDelay;fprintf (stderr, "\t pselect      %8u   %8u   %8d\n", nDelay, nTimeTest,nReduce);fprintf (stderr, "--------------------------------\n");}return 0;
}

参考博客:https://www.jianshu.com/p/42abcc2c9e50

这篇关于【Linux】延时函数sleep、usleep、nanosleep、select、pselect的比较的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/969408

相关文章

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

Python中4大日志记录库比较的终极PK

《Python中4大日志记录库比较的终极PK》日志记录框架是一种工具,可帮助您标准化应用程序中的日志记录过程,:本文主要介绍Python中4大日志记录库比较的相关资料,文中通过代码介绍的非常详细,... 目录一、logging库1、优点2、缺点二、LogAid库三、Loguru库四、Structlogphp

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

Linux下屏幕亮度的调节方式

《Linux下屏幕亮度的调节方式》文章介绍了Linux下屏幕亮度调节的几种方法,包括图形界面、手动调节(使用ACPI内核模块)和外接显示屏调节,以及自动调节软件(CaliseRedshift和Reds... 目录1 概述2 手动调节http://www.chinasem.cn2.1 手动屏幕调节2.2 外接显

Linux(centos7)虚拟机没有IP问题及解决方案

《Linux(centos7)虚拟机没有IP问题及解决方案》文章介绍了在CentOS7中配置虚拟机网络并使用Xshell连接虚拟机的步骤,首先,检查并配置网卡ens33的ONBOOT属性为yes,然后... 目录输入查看ZFhrxIP命令:ip addr查看,没有虚拟机IP修改ens33配置文件重启网络Xh

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合