国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

c語言定時器示例分享

2020-01-26 15:32:01
字體:
來源:轉載
供稿:網友

在linux下開發,使用的是C語言。適用于需要定時的軟件開發,以系統真實的時間來計算,它送出SIGALRM信號。每隔一秒定時一次

c語言定時器

復制代碼 代碼如下:

#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;                               //計數值
    unsigned int flag;                                //定時標志
}
;

struct StructOfTimer

    struct StructOfTimerStatus      testtime;   //測試定時器
}
mytime;

 

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


//定時器函數
/*******************************************************************************
* Discription:SIGALRM 信號響應函數;用作定時器
* Input      :
* Output    :
*******************************************************************************/
void SigalrmFunc(void)
{
    if(mytime.testtime.count++>20)      //定時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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 武川县| 陆河县| 保定市| 长宁区| 平南县| 庆元县| 高碑店市| 堆龙德庆县| 奉新县| 开阳县| 开封市| 永州市| 女性| 沙洋县| 石家庄市| 磐石市| 莎车县| 亚东县| 会同县| 融水| 雅江县| 铁岭县| 赣榆县| 邻水| 米脂县| 肥东县| 杭州市| 康马县| 米泉市| 安庆市| 东乌珠穆沁旗| 手机| 色达县| 吉安县| 同仁县| 金溪县| 白玉县| 织金县| 镇坪县| 弥渡县| 两当县|