Unix C 延时函数小结

阅读更多
在多线程的应用中要用到延时函数,开始时我只用到 sleep 这个秒级函数,但在 solaris  上跑时,程序运行到sleep时,却显示 “Alarm clock” 这句话后就中止了。据说是产生了 alarm 这个信号,而系统默认信号处理就是中止程序,所以要在程序中把这个设置为忽略:
signal(SIGALRM, SIG_IGN);


unix 上的延时函数有好几种:
引用

一、 基础知识
1、时间类型。Linux下常用的时间类型有4个:time_t,struct timeval,struct timespec,struct tm。
(1)time_t是一个长整型,一般用来表示用1970年以来的秒数。
(2)Struct timeval有两个成员,一个是秒,一个是微妙。
 struct timeval {
    long tv_sec;        /**//* seconds */
    long tv_usec;  /**//* microseconds */
};

(3)struct timespec有两个成员,一个是秒,一个是纳秒。
struct timespec{
    time_t  tv_sec;         /**//* seconds */
    long    tv_nsec;        /**//* nanoseconds */
};

(4)struct tm是直观意义上的时间表示方法:
struct tm {
    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_wday;        /**//* day of the week */
    int     tm_yday;        /**//* day in the year */
    int     tm_isdst;       /**//* daylight saving time */
};

2、 时间操作
(1) 时间格式间的转换函数
主要是 time_t、struct tm、时间的字符串格式之间的转换。看下面的函数参数类型以及返回值类型:
char *asctime(const struct tm *tm);
char *ctime(const time_t *timep);
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
time_t mktime(struct tm *tm);

gmtime和localtime的参数以及返回值类型相同,区别是前者返回的格林威治标准时间,后者是当地时间。
(2) 获取时间函数
两个函数,获取的时间类型看原型就知道了:
time_t time(time_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);

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


二、 延迟函数
主要的延迟函数有:sleep(),usleep(),nanosleep(),select(),pselect().
unsigned int sleep(unsigned int seconds);

void usleep(unsigned long usec);

int nanosleep(const struct timespec *req, struct timespec *rem);

int select(int n, fd_set *readfds,fd_set *writefds,fd_set *exceptfds,
struct timeval *timeout);

int 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时候 的测试结果,其他不贴了:
sleep           1000          0      -1000 
usleep           1000       2974       1974 
nanosleep        1000       2990       1990 
select           1000        991         -9 
pselect           1000        990        -10 
gettimeofday           1000       1000          0

而使用gettimeofday循环不停检测时间,可精确微秒级,不过不适宜用来做定时器模块。
因此后面的定时期模块将选择select为延迟函数。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wbj1234566/archive/2008/05/13/2442264.aspx


我在用 usleep 时却发现有部分线程完全在等待中,没有醒过来, 最后换用了 nanosleep
正常回了。注意,要调用 nanosleep, 编译时要带 -lposix4
nanosleep 的例子(来自http://hi.baidu.com/zengzhaonong/blog/item/2fa4a799e282bb096f068c62.html)
#include 
#include 
#include 
#include 
#include 
#define COUNT 1000
#define MILLION 1000000L

int main(void)
{
    int i;
    struct timespec slptm;
    long   tdif;
    struct timeval tend, tstart;

    slptm.tv_sec = 0;
    slptm.tv_nsec = 1000;      //1000 ns = 1 us

    //struct sched_param param;    
    //param.sched_priority = 0;
    //sched_setscheduler(getpid(), SCHED_FIFO, ¶m);

    if (gettimeofday(&tstart, NULL) == -1) {
        fprintf(stderr, "Failed to get start time\n");
        return 1;
    }
    for (i = 0; i < COUNT; i++) {
        if (nanosleep(&slptm, NULL) == -1) {
            perror("Failed to nanosleep");
            return 1;
        }
    }
    if (gettimeofday(&tend, NULL) == -1) {
        fprintf(stderr, "Failed to get end time\n");
        return 1;
    }
    tdif = MILLION * (tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);
    printf("nanosleep() time is %ld us\n", tdif/COUNT);
    return 0;
}



HZ                                 250HZ
时钟中断的时间间隔:                   4 ms   (1000ms/250)
----------------------------------------
nanosleep() time is 4019 us        (4.019 ms)
说明nanosleep的睡眠定时器依赖于时钟中断



HZ                                 1000HZ
时钟中断的时间间隔:                   1 ms
----------------------------------------
nanosleep() time is 12 us
注: 最小睡眠时间为1 us

你可能感兴趣的:(Unix,C,C++,C#,Linux)