Chinaunix

标题: 关于nanosleep的精度问题。 [打印本页]

作者: xb_parasite    时间: 2006-02-20 11:12
标题: 关于nanosleep的精度问题。
运行这样一个测试程序(书上的原样程序):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #define COUNT 100
  6. #define D_BILLION 1000000000.0
  7. #define D_MILLION 1000000.0
  8. #define MILLION 1000000L
  9. #define NANOSECONDS 1000

  10. int main(void) {
  11.    int i;
  12.    struct timespec slptm;
  13.    long tdif;
  14.    struct timeval tend, tstart;

  15.    slptm.tv_sec = 0;
  16.    slptm.tv_nsec = NANOSECONDS;
  17.    if (gettimeofday(&tstart, NULL) == -1) {
  18.       fprintf(stderr, "Failed to get start time\n");
  19.       return 1;
  20.    }
  21.    for (i = 0; i < COUNT; i++)
  22.       if (nanosleep(&slptm, NULL) == -1) {
  23.          perror("Failed to nanosleep");
  24.          return 1;
  25.       }
  26.    if (gettimeofday(&tend, NULL) == -1) {
  27.       fprintf(stderr, "Failed to get end time\n");
  28.       return 1;
  29.    }
  30.    tdif = MILLION*(tend.tv_sec - tstart.tv_sec) +
  31.                    tend.tv_usec - tstart.tv_usec;
  32.    printf("%d nanosleeps of %d nanoseconds\n", COUNT, NANOSECONDS);
  33.    printf("Should take   %11d microseconds or %f seconds\n",
  34.              NANOSECONDS*COUNT/1000, NANOSECONDS*COUNT/D_BILLION);
  35.    printf("Actually took %11ld microseconds or %f seconds\n", tdif,
  36.              tdif/D_MILLION);
  37.    printf("Number of seconds per nanosleep was       %f\n",
  38.              (tdif/(double)COUNT)/MILLION);
  39.    printf("Number of seconds per nanosleep should be %f\n",
  40.              NANOSECONDS/D_BILLION);
  41.    return 0;
  42. }
复制代码

在我的机器上的输出是:
100 nanosleeps of 1000 nanoseconds
Should take           100 microseconds or 0.000100 seconds
Actually took     2007089 microseconds or 2.007089 seconds
Number of seconds per nanosleep was       0.020071
Number of seconds per nanosleep should be 0.000001

似乎精度远远不够。怎么回事呢?
作者: albcamus    时间: 2006-02-20 13:27
你的硬件, OS极其版本?
作者: xb_parasite    时间: 2006-02-20 15:22
我的就是一般的pc机器配置。
在以下2台机器上运行结果相似。
centOS 3.6 (kernel 2.4.21-37.EL)
CPU:P4 2.8G
MEMORY: 1024M

100 nanosleeps of 1000 nanoseconds
Should take           100 microseconds or 0.000100 seconds
Actually took     1998414 microseconds or 1.998414 seconds
Number of seconds per nanosleep was       0.019984
Number of seconds per nanosleep should be 0.000001

debian linux (kernel 2.4.27-2)  on vmware5.0
CPU:P4 2.8G
MEMORY: 256M

100 nanosleeps of 1000 nanoseconds
Should take           100 microseconds or 0.000100 seconds
Actually took     2007089 microseconds or 2.007089 seconds
Number of seconds per nanosleep was       0.020071
Number of seconds per nanosleep should be 0.000001

您可以在你的牛xx服务器上跑跑,看是什么输出。
觉得精度差太大了。程序本身又找不出毛病。

Do any one  knows?

[[i] 本帖最后由 xb_parasite 于 2006-2-22 09:45 编辑 [/i]]
作者: goodtaste    时间: 2007-01-19 11:20
标题: nanosleep有10毫秒的误差
这是Linux进程切换的消耗时间,除非你设置成软实时调度,这个误差可以降低到2毫秒
所以你这个测试根本不可能得到期待的结果.
nanosleep其实只能提供毫秒级别的时间控制.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2