c語言定時(shí)器示例分享
在linux下開發(fā),使用的是C語言。適用于需要定時(shí)的軟件開發(fā),以系統(tǒng)真實(shí)的時(shí)間來計(jì)算,它送出SIGALRM信號(hào)。每隔一秒定時(shí)一次
c語言定時(shí)器
#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)文章
關(guān)于vector迭代器失效的幾種情況總結(jié)
下面小編就為大家?guī)硪黄P(guān)于vector迭代器失效的幾種情況總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
C語言實(shí)現(xiàn)最長(zhǎng)遞增子序列問題的解決方法
這篇文章主要介紹了C語言實(shí)現(xiàn)最長(zhǎng)遞增子序列問題的解決方法,采用遞歸的方法解決該問題,是非常經(jīng)典的一類算法,需要的朋友可以參考下2014-09-09
C++實(shí)現(xiàn)的大數(shù)相乘算法示例
這篇文章主要介紹了C++實(shí)現(xiàn)的大數(shù)相乘算法,結(jié)合實(shí)例形式分析了C++大數(shù)相乘的概念、原理及代碼實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù)
這篇文章主要為大家詳細(xì)介紹了C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
C++實(shí)現(xiàn)LeetCode(87.攪亂字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(87.攪亂字符串),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)查找二叉樹中和為某一值的所有路徑的示例
這篇文章主要介紹了C++實(shí)現(xiàn)查找二叉樹中和為某一值的所有路徑的示例,文中的方法是根據(jù)數(shù)組生成二叉排序樹并進(jìn)行遍歷,需要的朋友可以參考下2016-02-02
基于Matlab實(shí)現(xiàn)人工神經(jīng)網(wǎng)絡(luò)(ANN)回歸的示例詳解
這篇文章主要為大家詳細(xì)介紹了Matlab實(shí)現(xiàn)人工神經(jīng)網(wǎng)絡(luò)(ANN)回歸的相關(guān)資料,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02

