Linux下的定時器

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   io   檔案   ar   

以下摘自linux下的man檔案:(man  getitimer)

  #include  <sys/time.h>

  int  getitimer(int which,  struct itimerval * curr_value);

  int  setitimer(int which,  const struct itimerval  * new_value, struct itimerval  * old_value);

描述:

  Linux系統中提供為每個進程提供了三個間隔定時器,在不同的時間域的每一個遞減。 

       當任何定時器逾時,則發送一個訊號到該過程,並在定時器(可能)重新啟動。

  ITIMER_REAL: 即時遞減,時間到發送SIGALRM訊號;

  ITIMER_VIRTUAL:遞減只有當進程正在執行,並且期滿後可提供SIGVTALRM

  ITIMER_PROF: 當進程執行或者是系統為進程調度的時候,減少計數,時間到了,發出SIGPROF訊號,這個和ITIMER_VIRTUAL聯合,常用來計算系統核心程式的時間和使用者時間。

以下結果體中定義了定時器的值:

  struct itimerval

  {

    struct timerval it_interval;  //next value;

    struct timerval it_value;   //current value;

  };

  struct timeval

  {

    long tv_sec;   //seconds; 

    long tv_usec;  //microseconds;

  }

it_interval用來指定每隔多長時間執行任務, it_value用來儲存目前時間離執行任務還有多長時間。比如說, 你指定it_interval為2秒(微秒為0),開始的時候我們把it_value的時間也設定為2秒(微秒為0),當過了一秒, it_value就減少一個為1, 再過1秒,則it_value又減少1,變為0,這個時候發出訊號(告訴使用者時間到了,可以執行任務了),並且系統自動把it_value的時間重設為 it_interval的值,即2秒,再重新計數。

 1 #include <sys/time.h> 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <signal.h> 5 #include <string.h> 6 10 //收到訊號的回呼函數11 void prompt_info(int signo)12 {13     printf("hello world\n");14 }15 16 //建立訊號處理機制17 void init_sigaction()18 {19     struct sigaction tact;20     //本進程接收到訊號要執行的處理函數prompt_info21     tact.sa_handler = prompt_info;22     tact.sa_flags   = 0;23 24     //初始化訊號集25     sigemptyset(&tact.sa_mask);26 27     //建立訊號處理機制28     sigaction(SIGALRM, &tact, NULL);29 }30 31 void init_time()32 {33     //設定執行任務的時間間隔為2秒0微秒34     struct itimerval value;35     value.it_value.tv_sec  = 2;36     value.it_value.tv_usec = 0;37 38     //設定初始時間計數也為2秒0微秒39     value.it_interval = value.it_value;40     41     //設定計時器ITIMER_REAL42     setitimer(ITIMER_REAL, &value, NULL);43 }44 45 int main()46 {48     init_sigaction();49 50     init_time();51     while(1)52         ;53 54     return 0;55 }

使用ITMER_REAL定時器,每隔2秒鐘都會發送一個SIGALRM訊號,當主函數接收到了這個訊號之後,調用訊號處理函數 prompt_info在輸出time is running out這個字串。

對於ITIMER_VIRTUAL和ITIMER_PROF的使用方法類似,在setitimer裡面設定的定時器為 ITIMER_VIRTUAL的時候,並把sigaction裡面的SIGALRM改為SIGVTALARM,

而ITIMER_PROF對應SIGPROF。

 1 /* 2  *通過自己計算時間差的方法來定時 3  *獲得精確計算時間差,把time 函數換成gettimeofday 4  */ 5  6 #include <signal.h> 7 #include <string.h> 8 #include <unistd.h> 9 #include <time.h>10 #include <stdio.h>11 12 static time_t lasttime;13 14 void show_msg(int signo)15 {16     printf("hello world\n");17 }18 19 int main()20 {21     struct sigaction act;22     union sigval tsval;23     act.sa_handler = show_msg;24     act.sa_flags = 0;25         26     sigemptyset(&act.sa_mask);27     sigaction(50, &act, NULL);28     time(&lasttime);29         30     while(1)31     {   32         time_t nowtime;33         //擷取目前時間34         time(&nowtime);35 36         if(nowtime-lasttime>=2)37         {38             sigqueue(getpid(), 50, tsval);39             lasttime = nowtime;40         }41     }42 43     return 0;44 }

 

聯繫我們

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