Linux Timer Timer "Turn"

Source: Internet
Author: User
Tags epoll set time

Transferred from: https://www.jianshu.com/p/66b3c75cae81

TIMERFD is a timer interface provided by Linux for the user program, which is based on a file descriptor, which is timed out via a read-through event of the file descriptor and can be used for epoll/select. There are three main functions.

Header file: include <sys/timerfd.h>int timerfd_create (int clockid, int flags)

Function: Generate timer, return file descriptor.
Clockid:clock_monotonic or Clock_realtime, where clock_monotonic represents the time it takes for the system to restart to the present, and changing the system time has no effect on it. Clock_realtime represents the time from 1970.1.1 to the present, changing the system time changes the obtained value.
Flags:tfd_nonblock (non-blocking), tfd_cloexec (same as O_cloexec).
The file descriptor for the Return:timer.

int timerfd_settime (int tfd, int flags, const struct ITIMERSPEC *newvalue, struct itimerspec *oldvalue)

Function: Used to start or close the timer for the specified FD.
TFD:TIMERFD, returned by the Timerfd_create function.
Flags:1 indicates that the absolute time is set; 0 indicates the relative time.
NewValue: Specify a new time-out, or turn off the timer if Newvalue.it_value is not 0. If the Newvalue.it_interval is 0, the timer is timed only once, otherwise every set time expires once.
OldValue: NOT NULL returns the time-out before the timer is set.
Return: Failure returns-1.

struct TIMESPEC
{
time_t tv_sec; Seconds
Long tv_nsec; Na-Sec
}
struct ITIMERSPEC
{
struct Timespec it_interval; After the first timeout, every it_interval times out
struct Timespec it_value; First Time Out
}

int timerfd_gettime (int fd, struct itimerspec *curvalue)

Function: Used to get the time remaining from the next time-out. If the timer has expired (that is, more than It_value time) when called, and the timer is in loop mode (that is, It_interval is not 0), the timer starts again after the function is called.
FD:TIMERFD, returned by the Timerfd_create function.
Curvalue: Returns the remaining time from the next timeout.
Return: Failed return-1

Read TIMERFD

When the timer expires, TIMERFD is readable, returns an integer of type uint64_t, the number of timeouts (refers to how many timeouts are unread), if the timer does not have a timeout event, if the TIMERFD is blocked, read will block, if TIMERFD is non-blocking, return eagain error. If read is less than 8 bytes of data, the EINVAL error is returned.

Sample code
#Include<sys/timerfd.h>#Include<sys/epoll.h>#Include<unistd.h>#Include<stdint.h>#Include<iostream>UsingNamespaceStdConstint epoll_size =10;IntMain(int argc,char* argv[]) {int TfD, EPFD, Nfds;structEpoll_eventEventstructEpoll_eventevents[Epoll_size];Create TIMERFD, Clock_realtime for absolute time, tfd_nonblock for non-blocking TFD = Timerfd_create (Clock_realtime, Tfd_nonblock);if (TfD <0) {Cerr <<"Timerfd_create error!" <<EndlReturn-1; }structTimespecStartTime,IntervalTime; Starttime.tv_sec =0; Starttime.tv_nsec =1;Equivalent to immediate arrival time-out intervaltime.tv_sec =3;After the first timeout, the timeout is intervaltime.tv_nsec every three seconds =0;structItimerspecNewValue; Newvalue.it_value = StartTime; Newvalue.it_interval = IntervalTime;Set the time-out period and the relative timeif (Timerfd_settime (TFD,0, &newvalue,NULL) <0) {Cerr <<"Timerfd_settime error!" <<EndlReturn-1; }Use Epoll to monitor descriptor EPFD = epoll_create (epoll_size);if (EPFD <0) {Cerr <<"Epoll_create error!" <<EndlReturn-1; } EVENT.DATA.FD = tfd; Event.events = Epollin;if (Epoll_ctl (EPFD, Epoll_ctl_add, TfD, &event) <0) {Cerr <<"Epoll_ctl error!" <<EndlReturn-1; }uint64_t count =0;while (1) {//nonblocking wait Nfds = epoll_wait (EPFD, events, Epoll_size, 0); if (Nfds = 0) continue; for (int i = 0; i < Nfds; i++) {if (events[i].events & Epollin) {uint64_t data; Read (EVENTS[I].DATA.FD, &data, sizeof (uint64_t)); count + = data; cout << " read: "<< data << endl;}} return 0;}         

A little gift to walk around, to Jane book Follow me



agin719
Links: https://www.jianshu.com/p/66b3c75cae81
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

Linux Timer Timer "Turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.