c语言定时器示例分享_C语言教程-查字典教程网
c语言定时器示例分享
c语言定时器示例分享
发布时间:2016-12-28 来源:查字典编辑
摘要:在linux下开发,使用的是C语言。适用于需要定时的软件开发,以系统真实的时间来计算,它送出SIGALRM信号。每隔一秒定时一次c语言定时器...

在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 successn");

}

}

return 0;

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C语言学习
    热门C语言学习
    编程开发子分类