首页 > 系统相关 >Linux下centOS和redhat测试数据usleep_nanosleep_select

Linux下centOS和redhat测试数据usleep_nanosleep_select

时间:2022-11-25 11:34:23浏览次数:51  
标签:usleep centOS redhat tv nanosleep ----------------------------------------------


系统不一样,gcc版本也不一样。其中centOS刚租任不久的没有甚么用户进程运行。redhat测试服务器有用户进程在后台运行

//编译 gcc test.c
//运行 ./a.out
#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>

#define PRINT_USEAGE {\
fprintf(stderr,"\n Usage: %s usec ",argv[0]); \
fprintf(stderr,"\n\n");\
}

int main (int argc, char **argv)
{
unsigned int nTimeTestSec = 0; /* sec */
unsigned int nTimeTest = 0; /* usec */
struct timeval tvBegin;
struct timeval tvNow;
int ret = 0;
unsigned int nDelay = 0; /* usec */
fd_set rfds;
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; /* 误差 */

#if 0
if (argc < 2)
{
PRINT_USEAGE;
exit (1);
}
nDelay = atoi (argv[1]);
#endif

fprintf (stderr, "%18s%12s%12s%12s\n", "function", "time(usec)", "realTime",
"reduce");
fprintf (stderr,
"-------------------------------------------------------------------\n");

for (i = 0; i < 11; i++)
{
if (delay[i] < 0)
break;
nDelay = delay[i];

/* test usleep */
gettimeofday (&tvBegin, NULL);
ret = usleep (nDelay);
if (-1 == ret)
{
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 nanosleep */
gettimeofday (&tvBegin, NULL);
req.tv_sec = nDelay / 1000000;
req.tv_nsec = (nDelay % 1000000) * 1000;
ret = nanosleep (&req, NULL);
if (-1 == ret)
{
fprintf (stderr, "\t nanosleep %8u not support\n", nDelay);
}
else
{
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 select */
gettimeofday (&tvBegin, NULL);
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = nDelay;
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);

fprintf (stderr,
"-------------------------------------------------------------------\n");
}

return 0;
}




******************************************************

|

******************************************************

[xujiayu@VM_97_158_centos sleep_c]$  cat /proc/version
Linux version 3.10.0-327.36.3.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) ) #1 SMP Mon Oct 24 16:09:20 UTC 2016
[xujiayu@VM_97_158_centos sleep_c]$ man  gcc|grep "GCC(1)"
GCC(1)                                          GNU                                          GCC(1)
gcc-4.8.5                                    2015-06-23                                      GCC(1)

function  time(usec)    realTime      reduce
-------------------------------------------------------------------
usleep 500000 500104 104
nanosleep 500000 500095 95
select 500000 500636 636
-------------------------------------------------------------------
usleep 100000 100116 116
nanosleep 100000 100105 105
select 100000 100468 468
-------------------------------------------------------------------
usleep 50000 50090 90
nanosleep 50000 50114 114
select 50000 50099 99
-------------------------------------------------------------------
usleep 10000 10102 102
nanosleep 10000 10077 77
select 10000 10103 103
-------------------------------------------------------------------
usleep 1000 1076 76
nanosleep 1000 1074 74
select 1000 1072 72
-------------------------------------------------------------------
usleep 900 973 73
nanosleep 900 980 80
select 900 972 72
-------------------------------------------------------------------
usleep 500 597 97
nanosleep 500 569 69
select 500 572 72
-------------------------------------------------------------------
usleep 100 169 69
nanosleep 100 338 238
select 100 170 70
-------------------------------------------------------------------
usleep 10 77 67
nanosleep 10 76 66
select 10 76 66
-------------------------------------------------------------------
usleep 1 68 67
nanosleep 1 67 66
select 1 67 66
-------------------------------------------------------------------
usleep 0 67 67
nanosleep 0 67 67
select 0 1 1
-------------------------------------------------------------------


******************************************************

|

******************************************************

$  cat /proc/version


Linux version 2.6.18-308.el5 ([email protected]) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)) #1 SMP Fri Jan 27 17:17:51 EST 2012


$ man  gcc|grep "GCC(1)"


GCC(1)                                GNU                               GCC(1)


gcc-4.1.2                         2011-10-26                            GCC(1)


function  time(usec)    realTime      reduce
-------------------------------------------------------------------
usleep 500000 501316 1316
nanosleep 500000 501143 1143
select 500000 499117 -883
-------------------------------------------------------------------
usleep 100000 101011 1011
nanosleep 100000 101013 1013
select 100000 100012 12
-------------------------------------------------------------------
usleep 50000 50999 999
nanosleep 50000 50996 996
select 50000 50001 1
-------------------------------------------------------------------
usleep 10000 10992 992
nanosleep 10000 10993 993
select 10000 9993 -7
-------------------------------------------------------------------
usleep 1000 1989 989
nanosleep 1000 1992 992
select 1000 993 -7
-------------------------------------------------------------------
usleep 900 1989 1089
nanosleep 900 1993 1093
select 900 990 90
-------------------------------------------------------------------
usleep 500 990 490
nanosleep 500 991 491
select 500 997 497
-------------------------------------------------------------------
usleep 100 987 887
nanosleep 100 991 891
select 100 992 892
-------------------------------------------------------------------
usleep 10 992 982
nanosleep 10 991 981
select 10 993 983
-------------------------------------------------------------------
usleep 1 990 989
nanosleep 1 994 993
select 1 990 989
-------------------------------------------------------------------
usleep 0 991 991
nanosleep 0 992 992
select 0 2 2
-------------------------------------------------------------------






标签:usleep,centOS,redhat,tv,nanosleep,----------------------------------------------
From: https://blog.51cto.com/datrilla/5886069

相关文章

  • 【Linux】linux | CentOS7 | 命令行模式 | 图形化界面模式
    1、图形化界面->命令行1)键盘按键Ctrl+Alt+F32、命令行->图形化界面1)键入命令init5......
  • centos7使用cgroup对user进行资源限制
    1.概述本篇介绍在centos7.9(4核1G虚拟机)上使用cgroup对user的资源(cpu,memory)进行限制,达到对普通用户的资源利用限制跟root资源限制生效且效果不同。适用于多用户登陆节......
  • CentOS7.9 MySQL5.1 安装
    1.下载相关mysql源码包wgethttps://downloads.mysql.com/archives/get/p/23/file/mysql-5.1.63-linux-x86_64-glibc23.tar.gz2.安装相关依赖包yuminstallbisongcc......
  • centos7部署heartbeat3.0
    1.概述本篇介绍在centos7.9上快速部署heartbeat3.0.6,以便在后续学习工作中使用heartbeat作为高可用软件实施其他服务的高可用部署。centos7.9源码编译按转heartbeat,大致......
  • 在centos 7上部署安装docker
    一、docker概述docker资源地址官网:https://www.docker.com/文档地址:https://docs.docker.com/仓库地址:https://hub.docker.com/镜像(images):docker镜像就好比是一个模......
  • Python3基于Centos7的安装
    0x00环境Centos7.9,默认已安装python2.7.50x01依赖软件yuminstallwgetzlib-develbzip2-developenssl-develncurses-develreadline-develtk-develgccmakezl......
  • centos7简单登录redis
    登录数据库:redis_cli-h[host]-p[port]输入用户名、密码登录auth[username]password选择数据库selectindex12.1对key操作的命令keys*:查询所有的ke......
  • [Docker]How to run CentOS in Docker container
    dockerpullcentosdockerimagesdockerrun-d-i-p10000:80centosdockerps 为什么需要加-i的参数:Nothingisactivelyrunning.Thatmakessense,because......
  • centos安装php模块之后还是提示not found解决方法
    我的centos服务器是7.3版本,我装了php5.6版本,然后再装memcached等扩展的时候,一直用不了,方法没找到。在网上找了很多资料,都说重启nginx服务器,然而行不通。。。。正确做法应该......
  • win10装centos双系统之后,win10的启动项消失的解决方法
    在网上教程​​http://www.centoscn.com/image-text/setup/2014/0728/3365.html​​的指引下在win10的系统下安装完centos7之后,莫名其妙原来Windows的启动引导消失了......