Linux應用程式層的定時器Timer

來源:互聯網
上載者:User

 

  在linux下提供了兩種基本的Timer機制:alarm和settimer。

   1、alarm    #include <unistd.h>    unsigned int alarm(unsigned int seconds);    這是個最簡單的Timer,當調用了alarm(n)時,等待n秒後,就會觸發一次SIGALRM訊號,故需要在調用alarm函數前先設定好 SIGALRM訊號對應的處理函數才行,而當調用alarm(0)時,表示停止當前的timer處理,不要發出SIGALRM訊號。    傳回值:返回上一次調用alarm函數的剩餘秒好,若之前沒有調用alarm函數,則返回0。    例(第一次等待1秒觸發Timer,之後都是2秒觸發):     #include <iostream>    #include <unistd.h>    #include <signal.h>     using namespace std;     void my_alarm_handler(int a){       cerr<<"my_alarm_handler"<<endl;       alarm(2);//更改為2秒調用一次Timer    }     int main(){       signal( SIGALRM, my_alarm_handler );       alarm(1);        while(1){}        return 0;     }   2、settimer     #include <sys/time.h>    #define ITIMER_REAL 0    #define ITIMER_VIRTUAL 1    #define ITIMER_PROF 2     int getitimer(int which, struct itimerval *value);    int setitimer(int which, const struct itimerval *value,struct itimerval *ovalue);    settimer和gettimer函數都提供了三種類別的Timer供使用:    1)、ITIMER_REAL:以系統實際的時間來計算,觸發時會發出SIGALRM訊號。    2)、ITIMER_VIRTUAL:只計算進程的執行時間(在使用者態),觸發時會發出SIGVTALRM訊號。    3)、ITIMER_PROF:計算進程在使用者態和核心態的處理時間,觸發時會發出SIGPROF訊號。    通過第一個參數which來指定要使用哪一種Timer(ITIMER_REAL、ITIMER_VIRTUAL、ITIMER_PROF)。 settimer函數是用來設定對應的Timer的觸發時間是多少,而gettimer函數是用來擷取上一次Timer設定的時間。設定的時間是一個結構 體struct itimerval:     struct itimerval {      struct timeval it_interval;       struct timeval it_value;     };    struct timeval {      long tv_sec;       long tv_usec;     };    settimer由第二個參數value設定觸發時間,第三個參數ovalue用來擷取上一次settimer設定的itimerval值(該參數可以設 置為NULL)。對於itimerval裡面變數的值,當我們設定it_interval的值為0時,Timer只會觸發一次,而it_value設定為 0時則表示Timer結束。    傳回值:0為成功,-1為失敗。    例(第一次等待1秒觸發Timer,之後都是2秒觸發):     #include <iostream>    #include <sys/time.h>    #include <signal.h>     using namespace std;     void my_alarm_handler(int a){      cerr<<"test "<<endl;    }     int main(){      struct itimerval t;      t.it_interval.tv_usec = 0;      t.it_interval.tv_sec = 2;      t.it_value.tv_usec = 0;      t.it_value.tv_sec = 1;       if( setitimer( ITIMER_REAL, &t, NULL) < 0 ){        cerr<<"settimer error."<<endl;        return -1;      }      signal( SIGALRM, my_alarm_handler );       while(1){        sleep(2);      }      return 0;    }    通過上面的例子,我們可以知道對於linux內建Timer只能同一時間處理3個Timer,如果需要多個的話,那麼這就是個問題了。不過我們可以通過sleep函數或time函數來結合使用實現定時功能,具體可以參考:http://hi.baidu.com/adrain001/blog/item/60580bc40871d6a18226ace4.html    參考網址:       http://code.google.com/p/androidteam/wiki/TimerExample       http://hi.baidu.com/adrain001/blog/item/6d488b63ff13fe680c33fae6.html       http://www.ezloo.com/2008/05/linux_timer.html       http://blog.sina.com.cn/s/blog_66a6a5ec0100koc0.html       http://hi.baidu.com/susdisk/blog/item/03f70d35e8e2e182a61e1288.html   (本文轉自http://blog.sina.com.cn/s/blog_3e4774e30100puh4.html)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.