• <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 的使用介紹(轉(zhuǎn))

            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()設(shè)定timer,但setitimer()第二個參數(shù)是sturct,負責(zé)設(shè)定timeout時間,所以第36行到第 40行設(shè)定此struct。itimerval.it_value設(shè)定第一次執(zhí)行function所延遲的秒數(shù), itimerval.it_interval設(shè)定以后每幾秒執(zhí)行function,所以若只想延遲一段時間執(zhí)行function,只要設(shè)定 itimerval.it_value即可,若要設(shè)定間格一段時間就執(zhí)行function,則it_value和it_interval都要設(shè)定,否則 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 大龍 閱讀(440) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            av无码久久久久不卡免费网站 | 91久久福利国产成人精品| 久久久一本精品99久久精品66| 成人综合伊人五月婷久久| 精品久久久久久无码国产| 久久天天躁狠狠躁夜夜2020| 久久午夜伦鲁片免费无码| 久久99精品国产| 国内精品久久久久影院老司 | av色综合久久天堂av色综合在| 久久久久亚洲av无码专区| 66精品综合久久久久久久| 777午夜精品久久av蜜臀| 久久久青草久久久青草| 少妇熟女久久综合网色欲| 国产一区二区三精品久久久无广告| 偷偷做久久久久网站| 狠狠精品干练久久久无码中文字幕| 色综合久久无码中文字幕| 久久久久女教师免费一区| 欧美精品一本久久男人的天堂| 无码人妻久久一区二区三区免费丨 | 日本欧美久久久久免费播放网| 久久免费视频一区| 国产精品99久久久久久www| 97久久久精品综合88久久| 色狠狠久久AV五月综合| 久久AV高潮AV无码AV| 99久久综合国产精品免费| 午夜视频久久久久一区 | 久久久久国产精品| 国产精品99久久精品| 久久久久久午夜成人影院| 欧美一区二区三区久久综合| 精品久久久久久久国产潘金莲| 久久久午夜精品福利内容| 色综合久久夜色精品国产| 亚洲性久久久影院| 精品多毛少妇人妻AV免费久久 | 久久无码专区国产精品发布 | 九九精品久久久久久噜噜|