|
sleep/usleep是依照什么来实现的呢? 我看以前的帖子觉得是依照HZ的值决定的,再精确也应该不会超过1s/HZ,但是我实验了一下,发现似乎又不是这样的。
HZ为250:
root@myUbuntu:/mnt/lfs# cat /proc/interrupts | grep timer && sleep 1 && cat /proc/interrupts | grep timer
0: 308 IO-APIC-edge timer
LOC: 1913564 Local timer interrupts
0: 308 IO-APIC-edge timer
LOC: 1913805 Local timer interrupts
tseong@myUbuntu:~/my_progm/14_time$ cat us.c
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
main()
{
struct timeval tv;
struct timezone tz;
gettimeofday (&tv,&tz);
printf("sec %u usec %u\n",tv.tv_sec,tv.tv_usec);
usleep(2);
gettimeofday (&tv,&tz);
printf("sec %u usec %u\n",tv.tv_sec,tv.tv_usec);
}
tseong@myUbuntu:~/my_progm/14_time$ ./us
sec 1240331351 usec 159986
sec 1240331351 usec 160175
tseong@myUbuntu:~/my_progm/14_time$ ./us
sec 1240331360 usec 724019
sec 1240331360 usec 724103
tseong@myUbuntu:~/my_progm/14_time$ ./us
sec 1240331423 usec 375991
sec 1240331423 usec 376182
时间精度似乎比1s/250高得多, 为什么?
tseong@myUbuntu:~$ uname -a
Linux myUbuntu 2.6.24-24-generic #1 SMP Wed Mar 25 12:16:54 UTC 2009 i686 GNU/Linux
另一方面,观察不同HZ的kenel下发现确实和HZ高低值有关。 |
|