Linux 环境编程 用户层定时器使用二 timer_create的使用
生活随笔
收集整理的這篇文章主要介紹了
Linux 环境编程 用户层定时器使用二 timer_create的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用戶層定時(shí)器有兩種,一種是timerfd,另一種是timer_create,前者比較新,使用比較方便。
Linux環(huán)境編程 用戶層定時(shí)器使用一 timerfd的使用
https://blog.csdn.net/fuyuande/article/details/80658695
這里記錄一下timer_create的用法。
先介紹一下相關(guān)接口,再結(jié)合一個(gè)簡單的demo介紹一下使用
頭文件:#include <signal.h>#include <time.h>/* * 創(chuàng)建定時(shí)器* 創(chuàng)建POSIX定時(shí)器,不會(huì)被傳遞給子進(jìn)程。編譯時(shí)候增加編譯選項(xiàng) -lrt* 定時(shí)器ID存儲(chǔ)在timerid中,定時(shí)器ID在當(dāng)前進(jìn)程中是唯一的,除非定時(shí)器* 被刪除。初始化的時(shí)候定時(shí)器未啟動(dòng)。* clockid定義了定時(shí)器計(jì)時(shí)的方法,有如下幾個(gè)值:* CLOCK_REALTIME : 可設(shè)置的系統(tǒng)范圍的實(shí)時(shí)時(shí)鐘* CLOCK_MONOTONIC : 單調(diào)遞增的時(shí)鐘,系統(tǒng)啟動(dòng)后不會(huì)被改變* CLOCK_PROCESS_CPUTIME_ID : 用于測量當(dāng)前進(jìn)程(包括所有線程)CPU占用時(shí)間,包含用戶調(diào)用和系統(tǒng)調(diào)用,* CLOCK_THREAD_CPUTIME_ID : 用于測量當(dāng)前線程CPU占用時(shí)間,包含用戶調(diào)用和系統(tǒng)調(diào)用* 參數(shù)sevp指出該如何通知調(diào)用者定時(shí)器超時(shí)信息,根據(jù)sevp.sigev_notify字段,該字段有如下值* SIGEV_NONE : 定時(shí)器超時(shí)后不使用異步通知,可能的情況是使用timer_gettime來監(jiān)控定時(shí)器* SIGEV_SIGNAL : 一旦超時(shí),產(chǎn)生一個(gè)信號(hào),任何時(shí)候,至多只有一個(gè)信號(hào)會(huì)發(fā)送到隊(duì)列里面,可以使用timer_getoverrun來獲取超時(shí)次數(shù)* SIGEV_THREAD : 新建一個(gè)線程去處理,該線程執(zhí)行sigev_notif_function為入口函數(shù)* SIGEV_THREAD_ID : linux獨(dú)有,發(fā)出一個(gè)信號(hào),和SIG_NAL類似,只不過該信號(hào)發(fā)送到指定的線程。* NULL : 如果sevp被設(shè)置為NULL,相當(dāng)于SIGEV_SIGNAL,信號(hào)是SIGALRM* 返回值,成功返回0,失敗返回-1并將錯(cuò)誤碼設(shè)置到errno里 */int timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid);使用到的時(shí)間參數(shù)數(shù)據(jù)結(jié)構(gòu)體有下面兩該個(gè) struct timespec {time_t tv_sec; /* Seconds */long tv_nsec; /* Nanoseconds */ }; struct itimerspec {struct timespec it_interval; /* Timer interval */struct timespec it_value; /* Initial expiration */ };/** 啟動(dòng)/關(guān)閉定時(shí)器* itimerspec將時(shí)間分為秒和納秒的組合* 如果參數(shù)new_value->it_value不為0,則啟動(dòng)定時(shí)器。如果定時(shí)器已經(jīng)啟動(dòng)則覆蓋之前的定時(shí)器設(shè)置* 如果參數(shù)new_value->it_value為0則關(guān)閉定時(shí)器。* 參數(shù)new_value->it_interval用于定時(shí)器重啟的間隔,即當(dāng)定時(shí)器超時(shí)后下一次超時(shí)的間隔。* 參數(shù)flags為0的話默認(rèn)是相對時(shí)間,也可以使用TIMER_ABSTIME絕對時(shí)間* 成功返回0,失敗返回-1,并設(shè)置errno*/ int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec * old_value);/** 返回到下一次超時(shí)的時(shí)間間隔,如果返回0說明已經(jīng)超時(shí)*/ int timer_gettime(timer_t timerid, struct itimerspec *curr_value);/** 刪除定時(shí)器* 成功返回0,失敗返回-1并設(shè)置errno*/int timer_delete(timer_t timerid);下面是一個(gè)簡單例子,創(chuàng)建一個(gè)定時(shí)器,超時(shí)時(shí)間1s, 超時(shí)后發(fā)出信號(hào),超時(shí)5次后程序退出
/** Description : Linux用戶層定時(shí)器使用<二>定義一個(gè)每個(gè)1s觸發(fā)的定時(shí)器,定時(shí)器觸發(fā)后發(fā)出信號(hào)并打印,超過5次后程序自動(dòng)退出。* Author : mason* Date : 201808*/#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> #include <time.h>#define EXPIRE_MAX 5 static int expire_cnt;#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \} while (0)/* 定時(shí)器回調(diào)函數(shù) */ void timer_handler() { if (expire_cnt < EXPIRE_MAX){expire_cnt++;}printf("timer expire\r\n");return; } int main(int argc, char *argv[]) {timer_t timerid;struct sigevent sev;struct itimerspec its;/* 注冊信號(hào)回調(diào)函數(shù) */sev.sigev_notify = SIGEV_SIGNAL;sev.sigev_signo = SIGUSR1;sev.sigev_value.sival_ptr = &timerid;signal(SIGUSR1, timer_handler);/* 創(chuàng)建定時(shí)器 */if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1){errExit("timer_create");}/* 設(shè)置定時(shí)器時(shí)間參數(shù)超時(shí)1s */its.it_value.tv_sec = 1;its.it_value.tv_nsec = 0;its.it_interval.tv_sec = its.it_value.tv_sec;its.it_interval.tv_nsec = its.it_value.tv_nsec;/* 啟動(dòng)定時(shí)器 */if (timer_settime(timerid, 0, &its, NULL) == -1){errExit("timer_settime");}/* 超時(shí)5次后退出 */while (expire_cnt != 5) {sleep(1);}/* 刪除定時(shí)器 */timer_delete(timerid);exit(EXIT_SUCCESS); }timer_create和timerfd相比的話,接口復(fù)雜,需要使用到信號(hào)機(jī)制,實(shí)際使用的話建議使用timerfd。
參考資料:
1. man timer_create:?https://linux.die.net/man/2/timer_create
2.?POSIX定時(shí)器:timer_settime()
?
總結(jié)
以上是生活随笔為你收集整理的Linux 环境编程 用户层定时器使用二 timer_create的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pdf 激活(急速pdf激活码)
- 下一篇: Linux 内核定时器使用 二 高精度定