Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are both tv_sec and tv_usec significant in determining the duration of a timer?

Tags:

c

timer

The manual page getitimer(2) claims that

both tv_sec and tv_usec significant in determining the duration of a timer

It doesn't go on to say why that is. In many examples that I have come across tv_sec is simply set to 0, while tv_usec is given some reasonable value, or vis versa. Are these timers counting down simultaneously, or is the total countdown time tv_sec + tv_usec? Should I use both? Neither?

like image 733
Ziggy Avatar asked Feb 23 '12 05:02

Ziggy


2 Answers

The man page documents the timeval structure:

struct timeval {
    long tv_sec;                /* seconds */
    long tv_usec;               /* microseconds */
};

If you want to wait a whole number of seconds, you'd just set tv_sec. If you want to wait a portion of a second, you'd set tv_usec. If you want to wait 4.5 seconds, you'd set both of them to appropriate values (4 and 500000, respectively)

like image 82
Damien_The_Unbeliever Avatar answered Sep 23 '22 00:09

Damien_The_Unbeliever


Yes, the total time is the sum of both. tv_sec is the seconds. And tv_usec is microseconds beyond that.

like image 29
user1118321 Avatar answered Sep 22 '22 00:09

user1118321