系统不一样,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
-------------------------------------------------------------------