简体   繁体   中英

what does timer expiration value for periodic linux timer signify?

I have already gone through the man page to create, and start the timer.

http://man7.org/linux/man-pages/man2/timerfd_create.2.html

However, i am not sure the use of the field "it_value" of struct itimerspec other than arm(start) and disarm(stop).

Question: what happens when a non-zero value is specified for this field.The man page documents that non-zero will start the timer and documents as timer expiration period? what does a timer expiration mean? what is the effect of timer expiration?

To start(arm) a timer, one can give value ranging from 1 ns to value equal to timer interval(non-zero). what would be the difference and the expected behavior in these two cases

Method1: Make the timer expiration equal to interval

int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
int milliseconds = 50;// 50 ms for example
struct itimerspec timspec;
timspec.it_interval.tv_sec = 0;
timspec.it_interval.tv_nsec = milliseconds * 1000000;
timspec.it_value.tv_sec = timspec.it_interval.tv_sec;
timspec.it_value.tv_nsec = timspec.it_interval.tv_nsec;
int res = timerfd_settime(timerfd, 0, &timspec, 0);

Method 2: Timer expiration less then timer interval

int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
int milliseconds = 50;// 50 ms for example
struct itimerspec timspec;
timspec.it_interval.tv_sec = 0;
timspec.it_interval.tv_nsec = milliseconds * 1000000;
timspec.it_value.tv_sec = 0;
timspec.it_value.tv_nsec = 1;
int res = timerfd_settime(timerfd, 0, &timspec, 0);

However, i am not sure the use of the field "it_value" of struct itimerspec other than arm(start) and disarm(stop).

Apparently your interest is in the use of this field with the second argument to timerfd_settime() , as opposed to the interpretation of values set in the struct, if any, pointed to by the third argument, or filled in by timerfd_gettime() . It's all pretty much the same, though.

You seem to have focused on the wrong thing in keying in on arming and disarming the timer. As the manpage you linked puts it,

The new_value argument specifies the initial expiration and interval for the timer.

(emphasis added). That is, the it_value of a struct itimerspec conveys the amount of time until the next timer expiration (or the absolute time of that expiration, depending on the flags), except that both fields zero indicates that the timer will never expire -- it is disarmed -- rather than that it will expire immediately. The value conveyed by this member does not have any particular correlation to the value conveyed by the it_interval member.

It follows that timerfd_settime() can be used to arm a disarmed timer or disarm an armed one, depending on the it_value passed to it, but those are special outcomes of more general behavior. The general case is that it is used to change the amount of time before a timer next expires, and the time increment between subsequent expirations.

To start(arm) a timer, one can give value ranging from 1 ns to value equal to timer interval(non-zero).

... or more. The delay before the next expiration is not limited to the length of the interval. As an extreme case, the interval can be zero, so that the timer is disarmed after expiring once.

what would be the difference and the expected behavior in these two cases

Method1: Make the timer expiration equal to [nonzero] interval

The timer will first expire after the specified amount of time (which is equal to the interval). Each time it expires, it will be reset to the time given by the interval.

Method 2: [nonzero] Timer expiration less then [nonzero] timer interval

The timer will first expire after the specified amount of time (which is less than the interval). Each time it expires, it will be reset to the time given by the interval.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM