• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            linux上定時函數(shù) setitimer 的使用介紹(轉)

            setitimer()為Linux的API,并非C語言的Standard Library,setitimer()有兩個功能,一是指定一段時間后,才執(zhí)行某個function,二是每間格一段時間就執(zhí)行某個function,以下程序demo如何使用setitimer()。
            view plaincopy to clipboardprint?
            /*
             
             
             
            Filename    : timer.cpp
             
            Compiler    : gcc 4.1.0 on Fedora Core 5
             
            Description : setitimer() set the interval to run function
             
            Synopsis    : #include <sys/time.h>
             
                          int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
             
                          struct itimerval {
             
                            struct timerval it_interval;
             
                            struct timerval it_value;
             
                          };
             
             
             
                          struct timeval {
             
                            long tv_sec;
             
                            long tv_usec;
             
                          }            
             
            Release     : 11/25/2006
             
            */ 
             
            #include <stdio.h>    // for printf() 
             
            #include <unistd.h>   // for pause() 
             
            #include <signal.h>   // for signal() 
             
            #include <string.h>   // for memset() 
             
            #include <sys/time.h> // struct itimeral. setitimer() 
             
             
             
            void printMsg(int); 
             
             
             
            int main() { 
             
              // Get system call result to determine successful or failed 
             
              int res = 0; 
             
              // Register printMsg to SIGALRM 
             
              signal(SIGALRM, printMsg); 
             
               
             
              struct itimerval tick; 
             
              // Initialize struct 
             
              memset(&tick, 0, sizeof(tick)); 
             
              // Timeout to run function first time 
             
              tick.it_value.tv_sec = 1;  // sec 
             
              tick.it_value.tv_usec = 0; // micro sec. 
             
              // Interval time to run function 
             
              tick.it_interval.tv_sec = 1; 
             
              tick.it_interval.tv_usec = 0; 
             
              // Set timer, ITIMER_REAL : real-time to decrease timer, 
             
              //                          send SIGALRM when timeout 
             
              res = setitimer(ITIMER_REAL, &tick, NULL); 
             
              if (res) { 
             
                printf("Set timer failed!!\n"); 
             
              } 
             
             
             
              // Always sleep to catch SIGALRM signal 
             
              while(1) { 
             
                pause(); 
             
              } 
             
             
             
              return 0;   
             

             
             
             
            void printMsg(int num) { 
             
              printf("%s","Hello World!!\n"); 
             

            當setitimer()所執(zhí)行的timer時間到了,會呼叫SIGALRM signal,所以在第30行用signal()將要執(zhí)行的function指定給SIGALRM。 在第43行呼叫setitimer()設定timer,但setitimer()第二個參數(shù)是sturct,負責設定timeout時間,所以第36行到第 40行設定此struct。itimerval.it_value設定第一次執(zhí)行function所延遲的秒數(shù), itimerval.it_interval設定以后每幾秒執(zhí)行function,所以若只想延遲一段時間執(zhí)行function,只要設定 itimerval.it_value即可,若要設定間格一段時間就執(zhí)行function,則it_value和it_interval都要設定,否則 funtion的第一次無法執(zhí)行,就別說以后的間隔執(zhí)行了。 第36行和第39行的tv_sec為sec,第37行和40行為micro sec(0.001 sec)。 第43行的第一個參數(shù)ITIMER_REAL,表示以real-time方式減少timer,在timeout時會送出SIGALRM signal。第三個參數(shù)會存放舊的timeout值,如果不需要的話,指定NULL即可。 第47 行的pause(),命令系統(tǒng)進入sleep狀態(tài),等待任何signal,一定要用while(1)無窮循環(huán)執(zhí)行pause(),如此才能一直接收 SIGALRM signal以間隔執(zhí)行function,若拿掉while(1),則function只會執(zhí)行一次而已。

            posted on 2010-05-23 03:23 大龍 閱讀(443) 評論(0)  編輯 收藏 引用

            久久精品国产亚洲AV香蕉| 久久精品国产亚洲一区二区三区| 国产激情久久久久影院| 久久人人超碰精品CAOPOREN| 久久午夜福利无码1000合集| 国产成人精品综合久久久| 久久国产精品无码网站| 亚洲精品国产自在久久| 狠狠久久亚洲欧美专区| 久久久久无码中| 国产精品久久久久久搜索| 人妻系列无码专区久久五月天| 无码国内精品久久人妻蜜桃| 久久成人国产精品一区二区| 天堂久久天堂AV色综合| 亚洲Av无码国产情品久久| 精品久久人妻av中文字幕| 色综合久久天天综线观看| 国内精品久久久久久99蜜桃| 人妻无码精品久久亚瑟影视| 伊人久久大香线焦综合四虎| 国内精品伊人久久久久av一坑| 超级97碰碰碰碰久久久久最新| 亚洲国产精品热久久| 久久久噜噜噜www成人网| 久久人人添人人爽添人人片牛牛| 国产精品99久久久久久董美香| 精品国产VA久久久久久久冰| 久久天天躁狠狠躁夜夜avapp| 久久久久一本毛久久久| 色综合久久久久网| 久久国产乱子伦精品免费强| 久久久久国产一级毛片高清版| 久久久久久国产精品无码超碰| 国产亚洲精品久久久久秋霞| 欧美日韩久久中文字幕| 天堂无码久久综合东京热| 亚洲成av人片不卡无码久久| 久久午夜无码鲁丝片午夜精品| 久久一区二区三区99| 亚洲午夜久久久|