博客專欄

        EEPW首頁 > 博客 > setitimer函數使用筆記

        setitimer函數使用筆記

        發布人:電子禪石 時間:2021-11-16 來源:工程師 發布文章

        轉載

        setitimer函數使用筆記

        函數原型int setitimer(int which, const struct itimerval *value,
                             struct itimerval *ovalue);

        DESCRIPTION
               The  system  provides each process with three interval timers, each decrementing in a distinct time domain.  When any timer expires, a
               signal is sent to the process, and the timer (potentially) restarts.

               ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expiration.

               ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

               ITIMER_PROF    decrements both when the process executes and when the system is executing on behalf  of  the  process.   Coupled  with
                              ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space.  SIG-
                              PROF is delivered upon expiration.

               Timer values are defined by the following structures:

                    struct itimerval {
                        struct timeval it_interval; /* next value */
                        struct timeval it_value;    /* current value */
                    };
                    struct timeval {
                        long tv_sec;                /* seconds */
                        long tv_usec;               /* microseconds */
                    };

        #include <stdio.h>
        #include 
        <unistd.h>
        #include 
        <signal.h>
        #include 
        <string.h>
        #include 
        <sys/time.h>
        #include 
        <errno.h>

        void PrintMsg(int Num)
        {
            printf(
        "%s\n""Hello World");

            
        return;
        }

        int main(int argc, char* argv[])
        {
            signal(SIGALRM, PrintMsg);

            
        struct itimerval tick;
            tick.it_value.tv_sec 
        = 10;  //十秒鐘后將啟動定時器
            tick.it_value.tv_usec = 0;
            tick.it_interval.tv_sec  
        =1//定時器啟動后,每隔1秒將執行相應的函數
            tick.it_interval.tv_usec = 0;

            
        //setitimer將觸發SIGALRM信號
            int ret = setitimer(ITIMER_REAL, &tick, NULL);

            
        if ( ret != 0)
            {
                printf(
        "Set timer error. %s \n", strerror(errno) );

                
        return -1;
            }

            printf(
        "Wait!\n");

            getchar();

            
        return 0;
        }

        加群:1732700158


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞: linux

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 芒康县| 班玛县| 南通市| 永德县| 鄂温| 彰化县| 江北区| 蓬莱市| 翁牛特旗| 出国| 开平市| 孟村| 临西县| 南昌县| 麻栗坡县| 乌什县| 金山区| 赤水市| 偏关县| 方正县| 恩施市| 衡东县| 那坡县| 邹城市| 巴东县| 资溪县| 仙游县| 大冶市| 建平县| 孙吴县| 赤峰市| 伽师县| 临清市| 西城区| 兴安县| 疏附县| 将乐县| 桂东县| 晋中市| 吉安县| 杂多县|