linux新定時器:timefd及相關操作函數

來源:互聯網
上載者:User

timerfd是Linux為使用者程式提供的一個定時器介面。這個介面基於檔案描述符,通過檔案描述符的可讀事件進行逾時通知,所以能夠被用於select/poll的應用情境。

一,相關操作函數

#include <sys/timerfd.h>

int timerfd_create(int clockid, int flags);

int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);

int timerfd_gettime(int fd, struct itimerspec *curr_value);

二,timerfd_create

int timerfd_create(int clockid, int flags);

它是用來建立一個定時器描述符timerfd

第一個參數:clockid指定時間類型,有兩個值:

CLOCK_REALTIME :Systemwide realtime clock. 系統範圍內的系統時鐘

CLOCK_MONOTONIC:以固定的速率運行,從不進行調整和複位 ,它不受任何系統time-of-day時鐘修改的影響

第二個參數:flags可以是0或者O_CLOEXEC/O_NONBLOCK。

傳回值:timerfd(檔案描述符)

三,timerfd_settime

在講解該函數前,先理解兩個相關結構體:

                  tv_nsec;                timespec it_interval;                  timespec it_value;     

第二個結構體itimerspec就是timerfd要設定的逾時結構體,它的成員it_value表示定時器第一次逾時時間,it_interval表示之後的逾時時間即每隔多長時間逾時

int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);

作用:用來啟動或關閉有fd指定的定時器

參數:

fd:timerfd,有timerfd_create函數返回

fnew_value:指定新的逾時時間,設定new_value.it_value非零則啟動定時器,否則關閉定時器,如果new_value.it_interval為0,則定時器只定時一次,即初始那次,否則之後每隔設定時間逾時一次

old_value:不為null,則返回定時器這次設定之前的逾時時間

flags:1代表設定的是絕對時間;為0代表相對時間。

四,timerfd_gettime

int timerfd_gettime(int fd, struct itimerspec *curr_value);

此函數用於獲得定時器距離下次逾時還剩下的時間。如果調用時定時器已經到期,並且該定時器處於迴圈模式(設定逾時時間時struct itimerspec::it_interval不為0),那麼調用此函數之後定時器重新開始計時。

The it_value field returns the amount of time until the timer will next expire. If both fields of this structure are zero, then the timer is currently disarmed. This field always contains a relative value, regardless of whether the TFD_TIMER_ABSTIME flag was specified when setting the timer.

The it_interval field returns the interval of the timer. If both fields of this structure are zero, then the timer is set to expire just once, at the time specified by curr_value.it_value.

五,read讀取timefd逾時事件通知

read(2) If the timer has already expired one or more times since its settings were last modified using timerfd_settime(), or since the last successful read(2), then the buffer given to read(2) returns an unsigned 8-byte integer (uint64_t) containing the number of expirations that have occurred. (The returned value is in host byte order, i.e., the native byte order for integers on the host machine.)

If no timer expirations have occurred at the time of the read(2), then the call either blocks until the next timer expiration, or fails with the error EAGAIN if the file descriptor has been made nonblocking (via the use of the fcntl(2) F_SETFL operation to set the O_NONBLOCK flag).

A read(2) will fail with the error EINVAL if the size of the supplied buffer is less than 8 bytes.

當定時器逾時,read讀事件發生即可讀,返回逾時次數(從上次調用timerfd_settime()啟動開始或上次read成功讀取開始),它是一個8位元組的unit64_t類型整數,如果定時器沒有發生逾時事件,則read將阻塞若timerfd為阻塞模式,否則返回EAGAIN 錯誤(O_NONBLOCK模式),如果read時提供的緩衝區小於8位元組將以EINVAL錯誤返回。

 

六,範例程式碼

man手冊中樣本:

The following program creates a timer and then monitors its progress. The program accepts up to three command-line arguments. The first argument specifies the number of seconds for the initial expiration of the timer. The second argument specifies the interval for the timer, in seconds. The third argument specifies the number of times the program should allow the timer to expire before terminating. The second and third command-line arguments are optional.

#include <sys/timerfd.h><time.h><unistd.h><stdlib.h><stdio.h><stdint.h>         handle_error(msg) \        { perror(msg); exit(EXIT_FAILURE); }  (    first_call = =  (clock_gettime(CLOCK_MONOTONIC, &start) == - (clock_gettime(CLOCK_MONOTONIC, &curr) == -= curr.tv_sec -= curr.tv_nsec - (nsecs < --+= , secs, (nsecs + ) /  argc,  * ((argc != ) && (argc !=  (clock_gettime(CLOCK_REALTIME, &now) == -= now.tv_sec + atoi(argv[= (argc == = = = atoi(argv[= atoi(argv[= = timerfd_create(CLOCK_REALTIME,  (fd == - (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == - (tot_exp = ; tot_exp <= read(fd, &exp,  (s != +=  

運行結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.