Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562002
  • 博文数量: 192
  • 博客积分: 3780
  • 博客等级: 中校
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:11
文章存档

2012年(6)

2011年(160)

2010年(26)

分类: 嵌入式

2011-08-27 14:31:03

1、时间类型。Linux下常用的时间类型有4个:time_t,struct timeval,struct timespec,struct tm。

(1)time_t是一个长整型,一般用来表示用1970年以来的秒数。

(2)Struct timeval有两个成员,一个是秒,一个是微妙。

struct timeval linux-时间相关结构体和函数 - Teana - Teana{

            long tv_sec;        /**//* seconds */

            long tv_usec;  /**//* microseconds */

 };

(3)struct timespec有两个成员,一个是秒,一个是纳秒。

struct timespeclinux-时间相关结构体和函数 - Teana - Teana{

         time_t  tv_sec;         /**//* seconds */

         long    tv_nsec;        /**//* nanoseconds */

 };

(4)struct tm是直观意义上的时间表示方法:

struct tm linux-时间相关结构体和函数 - Teana - Teana{

        int     tm_sec;         /**//* seconds */

        int     tm_min;         /**//* minutes */

        int     tm_hour;        /**//* hours */

        int     tm_mday;        /**//* day of the month */

        int     tm_mon;         /**//* month */

        int     tm_year;        /**//* year */

        int     tm_yday;        /**//* day in the year */

        int     tm_wday;         /**//* day of the week */

        int     tm_isdst;       /**//* daylight saving time */

 };

2、 时间操作

(1) 时间格式间的转换函数

主要是 time_t、struct tm、时间的字符串格式之间的转换。看下面的函数参数类型以及返回值类型:

linux-时间相关结构体和函数 - Teana - Teanachar *asctime(const struct tm *tm);

linux-时间相关结构体和函数 - Teana - Teanachar *ctime(const time_t *timep);

linux-时间相关结构体和函数 - Teana - Teanastruct tm *gmtime(const time_t *timep);

linux-时间相关结构体和函数 - Teana - Teanastruct tm *localtime(const time_t *timep);

linux-时间相关结构体和函数 - Teana - Teanatime_t mktime(struct tm *tm);

gmtime和localtime的参数以及返回值类型相同,区别是前者返回的格林威治标准时间,后者是当地时间。

(2) 获取时间函数

两个函数,获取的时间类型看原型就知道了:

linux-时间相关结构体和函数 - Teana - Teanatime_t time(time_t *t);

linux-时间相关结构体和函数 - Teana - Teanaint gettimeofday(struct timeval *tv, struct timezone *tz);

前者获取time_t类型,后者获取struct timeval类型,因为类型的缘故,前者只能精确到秒,后者可以精确到微秒。

二、 延迟函数

主要的延迟函数有:sleep(),usleep(),nanosleep(),select(),pselect().

linux-时间相关结构体和函数 - Teana - Teanaunsigned int sleep(unsigned int seconds);

linux-时间相关结构体和函数 - Teana - Teanavoid usleep(unsigned long usec);

linux-时间相关结构体和函数 - Teana - Teanaint nanosleep(const struct timespec *req, struct timespec *rem);

linux-时间相关结构体和函数 - Teana - Teanaint select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,struct timeval *timeout);

linux-时间相关结构体和函数 - Teana - Teanaint pselect(int   n,   fd_set   *readfds,  fd_set  *writefds,  fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);

alarm函数是信号方式的延迟,这种方式不直观,这里不说了。

仅通过函数原型中时间参数类型,可以猜测sleep可以精确到秒级,usleep/select可以精确到微妙级,nanosleep和pselect可以精确到纳秒级。

而实际实现中,linux上的nanosleep和alarm相同,都是基于内核时钟机制实现,受linux内核时钟实现的影响,并不能达到纳秒级的精度,man nanosleep也可以看到这个说明,man里给出的精度是:Linux/i386上是10 ms ,Linux/Alpha上是1ms。

这里有一篇文章http://blog.csdn.net/zhoujunyi/archive/2007/03/30/1546330.aspx,测试了不同延迟函数之间的精确度。文章给出的结论是linux上精度最高的是select,10ms级别。我在本机器测试select和pselect相当,都达到了1ms级的精度,精度高于文章中给出的10ms,sleep在秒级以上和usleep/nanosleep相当。下面贴下我机器上1ms时候的测试结果,其他不贴了:

linux-时间相关结构体和函数 - Teana - Teanasleep           1000          0      -1000 

linux-时间相关结构体和函数 - Teana - Teanausleep           1000       2974       1974 

linux-时间相关结构体和函数 - Teana - Teanananosleep        1000       2990       1990 

linux-时间相关结构体和函数 - Teana - Teanaselect           1000        991         -9 

linux-时间相关结构体和函数 - Teana - Teanapselect           1000        990        -10 

linux-时间相关结构体和函数 - Teana - Teanagettimeofday           1000       1000          0

而使用gettimeofday循环不停检测时间,可精确微秒级,不过不适宜用来做定时器模块。

因此后面的定时期模块将选择select为延迟函数。

阅读(472) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~