linux 间隔定时器,Linux间隔定时器的使用 探索一
2011年1月17日
之前看《高級Unix編程》說有基本定時器與高級定時器之分
好像基本定時器不符合我的要求,那么就先來個高級的吧。
寫個代碼看看會有什么發生:
2011年1月18日
看下timer_create函數
intt1;
timer_t tm_id;//timer_t其實是個long型t1 = timer_create(CLOCK_REALTIME, NULL, &tm_id);
cout << "timer_create return "<< t1 << endl;
cout << "tm_id"<< tm_id << endl;
intt2;
timer_t tm_id2;//timer_t其實是個long型t2 = timer_create(CLOCK_REALTIME, NULL, &tm_id2);
cout << "timer_create return "<< t2 << endl;
cout << "tm_id"<< tm_id2 << endl;
可看到輸出的第一個time_id是0,第二個是1,其實就是用數字標識timerID啦。
2011年1月21日
今天終于試驗出了個timer的用法:
下面是代碼:
#include#include#includeusingstd::cout;
usingstd::endl;
voidalrm_handler(inti)
{
printf("timer interrupt %d/n",i);
time_t tm_show;
time(&tm_show);
cout << "settime : "<< (long)tm_show << "s "<< endl;
}
intmain()
{
intt1;
timer_t tm_id;//timer_t其?實?是?個?long型?t1 = timer_create(CLOCK_REALTIME, NULL, &tm_id);
cout << "timer_create return "<< t1 << endl;
cout << "tm_id"<< tm_id << endl;
intt2;
timer_t tm_id2;//timer_t其?實?是?個?long型?t2 = timer_create(CLOCK_REALTIME, NULL, &tm_id2);
cout << "timer_create return "<< t2 << endl;
cout << "tm_id"<< tm_id2 << endl;
cout << "set the timer "<< tm_id << ": 5s"<< endl;
cout << "set timer "<< tm_id2 << ": 3s"<< endl;
signal(SIGALRM,alrm_handler); //<>說?最?好?不?要?用?signal,?管?它?呢?,?這?個?簡?單?,?用?sigacttion也?太?煩?了?吧?
//set timer 0 5sstructitimerspec itmspec;
structtimespec ts;
//struct sigaction sa;
//sigfillset(&sa.sa_mask);
//sa.sa_flags = SA_SIGINFO;
//sa.sa_sigaction = handler;itmspec.it_interval.tv_sec = 5;
itmspec.it_interval.tv_nsec = 0;
itmspec.it_value.tv_sec = 5;
itmspec.it_value.tv_nsec = 0;
time_t tm_show;
time(&tm_show);
timer_settime(tm_id, NULL,&itmspec,NULL);//第?二?個?參?數?為?,?則?與?setitimer相?似?,?第?四?個?參?數?返?回?舊?的?定?時?器?值?cout << "settime : "<< (long)tm_show << "s "<< endl;
itmspec.it_interval.tv_sec = 3;
itmspec.it_interval.tv_nsec = 0;
itmspec.it_value.tv_sec = 3;
itmspec.it_value.tv_nsec = 0;
timer_settime(tm_id2, NULL,&itmspec,NULL);
while(1);
return0;
}
makefile文件:
# # # # # # # # # # # # # # # # # # # # #
#? Makefile for pafone, general use
#?????????????????????? 2011.01.18
# # # # # # # # # # # # # # # # # # # # #
LIBS = -lrt
OBJS = main.o
TARGET = timer
#GCFLAGS :compile flags, GDFLAGS: link flags
GCFLAGS =
GDFLAGS =
GCC? = g++
all : $(OBJS)
$(GCC) -o $(TARGET) $(OBJS) $(LIBS)
@echo "compile success"
%.o : %.cpp
$(GCC) $(GCFLAGS) -c $< -o $@
clean :
rm -f $(TARGET) $(OBJS)
編譯運行后的輸出:
[root@pafone timer]# ./timer
timer_create return 0
tm_id0
timer_create return 0
tm_id1
set the timer 0: 5s
set timer 1: 3s
settime : 1295597124s
timer interrupt 14
settime : 1295597127s
timer interrupt 14
settime : 1295597129s
timer interrupt 14
settime : 1295597130s
timer interrupt 14
settime : 1295597133s
其中有一個是5s,另一個是3s的,但alrm_handler(inti)? 函數傳入來的i都是14,怎么區分是哪個定時器呢???? man 7 signal 發現里面的說明有說到handler的傳入參數便是信號SIGALRM的#define值。
那么用sigacton可不可以區分是哪個定時器呢?還是一定要通過發送不同的信號來區分定時器?
注:信號會中斷一些阻塞調用及sleep()函數,例如將while(1)改為sleep()
itmspec.it_interval.tv_sec = 3;
itmspec.it_interval.tv_nsec = 0;
itmspec.it_value.tv_sec = 3;
itmspec.it_value.tv_nsec = 0;
timer_settime(tm_id2, NULL,&itmspec,NULL);
sleep(100);
輸出是:
[root@pafone timer]# ./timer
timer_create return 0
tm_id0
timer_create return 0
tm_id1
set the timer 0: 5s
set timer 1: 3s
settime : 1295599530s
timer interrupt 14
settime : 1295599533s
[root@pafone timer]#
即它會在第一個定時信號后返回,也就是定時信號中斷了sleep.
如用用sigaction, 其它一些系統調用可以設置SA_RESTART使調用繼續,但對sleep不起作用。
總結
以上是生活随笔為你收集整理的linux 间隔定时器,Linux间隔定时器的使用 探索一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 百度ocr安装_Pytho
- 下一篇: 【Python基础知识-pycharm版