導航:首頁 > 編程系統 > linux定時器處理函數

linux定時器處理函數

發布時間:2023-03-05 15:13:02

linux下C語言定時器(求高人指點)

可以用alarm信號做:

alarm(設置信號傳送鬧鍾)
相關函數 signal,sleep

表頭文件 #include<unistd.h>

定義函數 unsigned int alarm(unsigned int seconds);

函數說明 alarm()用來設置信回號SIGALRM在經過參數seconds指定的秒答數後傳送給目前的進程。如果參數seconds 為0,則之前設置的鬧鍾會被取消,並將剩下的時間返回。

返回值返回之前鬧鍾的剩餘秒數,如果之前未設鬧鍾則返回0。

#include<unistd.h>
#include<signal.h>
void handler() {
//這里讀跳變次數
}
main()
{
int i;
signal(SIGALRM,handler);//這里設置時鍾信號的響應函數
alarm(1); //這里設置每一秒鍾發送一個時鍾信號
}

⑵ Linux下的定時器,怎麼用

數為秒數,在經過指定秒數後,alarm會發出一個SIGALRM信號
singal函數用來綁定信號處理器函專數,這里綁定的是屬timer,被綁定的函數必須固定為返回值void、參數int.
只需要alarm(時間)就設置了,可能由於getchar需要進入中斷導致信號被掛起所以沒反應,可以試試把getchar換成別的東西來延時看看。
關於更多Linux的學習,請查閱書籍《linux就該這么學》。

⑶ 怎樣在Linux下實現精確定時器

linux下使用select實現精確定時器
在編寫程序時,我們經常回用到定時器。本文講述如何使用select實現超級時鍾。使用select函數,我們能實現微妙級別精度的定時器。同時,select函數也是我們在編寫非阻塞程序時經常用到的一個函數。
首先看看select函數原型如下:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);

參數說明:
slect的第一個參數nfds為fdset集合中最大描述符值加1,fdset是一個位數組,其大小限制為__FD_SETSIZE(1024),位數組的每一位代表其對應的描述符是否需要被檢查。
select的第二三四個參數表示需要關注讀、寫、錯誤事件的文件描述符位數組,這些參數既是輸入參數也是輸出參數,可能會被內核修改用於標示哪些描述符上發生了關注的事件。所以每次調用select前都需重新初始化fdset。
timeout參數為超時時間,該結構會被內核修改,其值為超時剩餘的時間。
利用select實現定時器,需要利用其timeout參數,注意到:
1)select函數使用了一個結構體timeval作為其參數。
2)select函數會更新timeval的值,timeval保持的值為剩餘時間。
如果我們指定了參數timeval的值,而將其他參數都置為0或者NULL,那麼在時間耗盡後,select函數便返回,基於這一點,我們可以利用select實現精確定時。
timeval的結構如下:
struct timeval{
long tv_sec;/*secons*
long tv_usec;/*microseconds*/
}

我們可以看出其精確到microseconds也即微妙。
一、秒級定時器

void seconds_sleep(unsigned seconds){
struct timeval tv;
tv.tv_sec=seconds;
tv.tv_usec=0;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

二、毫秒級別定時器

void milliseconds_sleep(unsigned long mSec){
struct timeval tv;
tv.tv_sec=mSec/1000;
tv.tv_usec=(mSec%1000)*1000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

三、微妙級別定時器

void microseconds_sleep(unsigned long uSec){
struct timeval tv;
tv.tv_sec=uSec/1000000;
tv.tv_usec=uSec%1000000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

現在我們來編寫幾行代碼看看定時效果吧。

#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
int main()
{
int i;
for(i=0;i<5;++i){
printf("%d\n",i);
//seconds_sleep(1);
//milliseconds_sleep(1500);
microseconds_sleep(1900000);
}
}

註:timeval結構體中雖然指定了一個微妙級別的解析度,但內核支持的分別率往往沒有這么高,很多unix內核將超時值向上舍入成10ms的倍數。此外,加上內核調度延時現象,即定時器時間到後,內核還需要花一定時間調度相應進程的運行。因此,定時器的精度,最終還是由內核支持的分別率決定。

⑷ 在linux C編程中,定時器函數選擇與設置問題

試來試alarm()與自signal(),例子可以網上搜搜

NAME
alarm - set an alarm clock for delivery of a signal

SYNOPSIS
#include <unistd.h>

unsigned int alarm(unsigned int seconds);

DESCRIPTION
alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds.

If seconds is zero, no new alarm() is scheled.

In any event any previously set alarm() is canceled.

⑸ linux下多個定時器的實現(C語言),麻煩高手指點哈嘛(急)

給你兩個函數參考
omsTimer函數是處理定時事件,void(*handle)(union sigval v)參數就是處理事件的函數指針。
int omsSetTimer(timer_t *tId,int value,int interval)就是設置定時器。
按你說的,如果要同時起多個定時器,需要定義一個數組timer_t tm[n];int it[n];tm就是定時器結構,it用來記錄對應的定時器是否已經使用,使用中的就是1,沒用的就是0;
主進程消息來了就從it找一個沒用的來omsSetTimer,如果收到終止消息,那omsSetTimer 定時時間為0
int omsTimer(timer_t *tId,int iValue,int iSeconds ,void(*handle)(union sigval v),void * param)
{
struct sigevent se;
struct itimerspec ts;
memset (&se, 0, sizeof (se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_ptr = param;
if (timer_create (CLOCK_REALTIME, &se, tId) < 0)
{
return -1;
}
ts.it_value.tv_sec = iValue;
// ts.it_value.tv_sec =3;
//ts.it_value.tv_nsec = (long)(iValue % 1000) * (1000000L);
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = iSeconds;
//ts.it_interval.tv_nsec = (long)(iSeconds % 1000) * (1000000L);
ts.it_interval.tv_nsec = 0;
if (timer_settime(*tId, TIMER_ABSTIME, &ts, NULL) < 0)
{
return -1;
}
return 0;
}
int omsSetTimer(timer_t *tId,int value,int interval)
{
struct itimerspec ts;
ts.it_value.tv_sec =value;
//ts.it_value.tv_nsec = (long)(value % 1000) * (1000000L);
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = interval;
//ts.it_interval.tv_nsec = (long)(interval % 1000) * (1000000L);
ts.it_interval.tv_nsec = 0;
if (timer_settime(*tId, TIMER_ABSTIME, &ts, NULL) < 0)
{
return -1;
}
return 0;
}

⑹ linux下C語言有沒有定時調用的函數


你是指延遲函數還是定時調用,比如到11:12:13秒執行某個函數?

如果是延時函數用

usleep(usec);//微妙
sleep(sec);//秒

如果是定時執行的話,你只能開個線程或while,每隔1s判斷下當前系統時間,

structtm*ptm;
longts;
inty,m,d,h,n,s;

ts=time(NULL);
ptm=localtime(&ts);

y=ptm->tm_year+1900;//年
m=ptm->tm_mon+1;//月
d=ptm->tm_mday;//日
h=ptm->tm_hour;//時
n=ptm->tm_min;//分
s=ptm->tm_sec;//秒

照著上面的格式組成字元串進行判斷,到點了就執行就可以了

⑺ 有誰會用linux裡面的定時器timer_list

這段代碼是Linux的一個內核模塊程序,timer_list也是Linux內核中的數據結構。模塊程序不是以main函數作為入口的。而是以mole_init指定。
mole_init/mole_exit分別用於指定模塊的載入和卸載函數。
載入模塊(insmod)時,會調用mytimer_init函數。這個函數設置一個定時器,在定時器超時時執行myfunc函數,指定函數參數為「Hello,world!」。
myfunc執行時,會輸出「Hello,world!」,mod_timer函數會將定時器重新計時,兩秒後到期。

因此整個代碼執行起來後的現象就是每兩秒輸出一次Hello,world!

閱讀全文

與linux定時器處理函數相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接