Developed under Linux, the C language is used. It is suitable for the software development that needs to be timed, it sends out the SIGALRM signal with the real time of the system. Timed every second.
C Language Timer
Copy Code code as follows:
#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; Count value
unsigned int flag; Timing Flag
}
;
struct Structoftimer
{
struct Structoftimerstatus testtime; Test timer
}
MyTime;
void SetTimer (int sec,int usec);
void Sigalrmfunc (void);
//Timer function
/*******************************************************************************
* DISCRIPTION:SIGALRM signal response function; use as Timer
* input :
* output :
* * * /
void Sigalrmfunc (void)
{
if (mytime.testtime.count++>20) //timed 1 seconds, 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;
}