欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c語(yǔ)言定時(shí)器示例分享

 更新時(shí)間:2014年04月28日 10:47:11   作者:  
在linux下開(kāi)發(fā),使用的是C語(yǔ)言。適用于需要定時(shí)的軟件開(kāi)發(fā),以系統(tǒng)真實(shí)的時(shí)間來(lái)計(jì)算,它送出SIGALRM信號(hào)。每隔一秒定時(shí)一次

在linux下開(kāi)發(fā),使用的是C語(yǔ)言。適用于需要定時(shí)的軟件開(kāi)發(fā),以系統(tǒng)真實(shí)的時(shí)間來(lái)計(jì)算,它送出SIGALRM信號(hào)。每隔一秒定時(shí)一次

c語(yǔ)言定時(shí)器

復(fù)制代碼 代碼如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "pthread.h"
#include <netinet/in.h>
#include <signal.h>
#include <sys/time.h>


struct StructOfTimerStatus
{
    unsigned int count;                               //計(jì)數(shù)值
    unsigned int flag;                                //定時(shí)標(biāo)志
}
;

struct StructOfTimer

    struct StructOfTimerStatus      testtime;   //測(cè)試定時(shí)器
}
mytime;

 

void SetTimer(int sec,int usec);
void SigalrmFunc(void);


//定時(shí)器函數(shù)
/*******************************************************************************
* Discription:SIGALRM 信號(hào)響應(yīng)函數(shù);用作定時(shí)器
* Input      :
* Output    :
*******************************************************************************/
void SigalrmFunc(void)
{
    if(mytime.testtime.count++>20)      //定時(shí)1秒,20*50000=1s
    {
        mytime.testtime.flag=1;
        mytime.testtime.count=0;
    }
}


void SetTimer(int sec,int usec)
{
    struct itimerval value,ovalue;
    signal(SIGALRM,(void *)SigalrmFunc);

    value.it_value.tv_sec = sec;
    value.it_value.tv_usec = usec;
    value.it_interval.tv_sec = sec;
    value.it_interval.tv_usec = usec;

    setitimer(ITIMER_REAL,&value,&ovalue); 
}

int main(int argc, char **argv)
{
    SetTimer(0, 50000);
    while(1)
    {
        if(mytime.testtime.flag == 1)
        {
            mytime.testtime.flag = 0;
            system("clear");
            printf("Timing success\n");
        }
    }
    return 0;
}

相關(guān)文章

最新評(píng)論