Introduction to Linux time functions and timers

1. Linux time function

(1) Related functions

#include<sys/time.h>

struct timeval
{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};

int gettimeofday(struct timeval *tv,struct timezone *tz)
//函数调用成功返回0,失败返回-1

(2) Function: gettimeofday() will return the current time with the tv structure, and put the local time zone information into the structure pointed to by tz. The second parameter is a time zone structure, which is outdated, so the tz parameter Usually should be specified as NULL .

(3) Code example: calculate how long the program takes to count 100,000 times in the current environment

#include <sys/time.h>
#include <stdio.h>

void mydelay()  //数10万次
{
    int i,j;
    for(i=0;i<100;i++){
        for(j=0;j<1000;j++);
    }
}

int main()
{
    struct timeval startTime;
    struct timeval stopTime;
    gettimeofday(&startTime,NULL);
    mydelay();
    gettimeofday(&stopTime,NULL);

    long diffTime = 1000000*(stopTime.tv_sec - startTime.tv_sec) +
    (stopTime.tv_usec - startTime.tv_usec);
    printf("全志H6的Linux数100000耗时%ldus\n",diffTime);
    
    return 0;
}

2. Linux timer

(1) Related functions

#include<sys/time.h>

struct itimerval
{
    /* Value to put into `it_value' when the timer expires. */
    struct timeval it_interval;
    /* Time to the next timer expiration. */
    struct timeval it_value;
};

/*it_interval:计时器的初始值,设定定时器的时间
  it_value:设定定时器开始生效的时间,延迟多长时间执行定时器*/

struct timeval
{
    __time_t tv_sec; /* Seconds. */
    __suseconds_t tv_usec; /* Microseconds. */
};
int setitimer (__itimer_which_t __which, /*设置定时方式*/
               const struct itimerval *__restrict __new, /*设置定时器的属性,传入struct itimerval结构体*/
               struct itimerval *__restrict __old) /*记录上一次的定时的时间参量,一般不使用,指定NULL/

setitimer() sets the structure pointed to by value as the current value of the timer. If ovalue is not NULL, it will return the original value of the timer.

which: three types

ITIMER_REAL: When the value is 0, the value of the timer is decremented in real time, and the signal sent is SIGALRM .

ITIMER_VIRTUAL: The value is 1, the value of the timer is decremented when the process is executed, and the signal sent is SIGVTALRM . ITIMER_PROF: The value is 2, the value of the timer is decremented when the process and the system are executed, and the signal sent is SIGPROF .

Obviously, the corresponding signal needs to be captured here for logic related processing signal(SIGALRM,signal_handler);

Return description: When executed successfully, return 0. Return -1 on failure

(2) Code example: print a hello every second

#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>

static int i;

void signal_handler(int signum)
{
     i++;
     if(i == 2000){ /*数2000个500us*/
         printf("hello\n");
         i = 0;
    }
}

int main()
{
     struct itimerval itv;

     /*设置定时时间*/
     itv.it_interval.tv_sec = 0;
     itv.it_interval.tv_usec = 500;
     /*设置开始生效,定时器启动时间*/
     itv.it_value.tv_sec = 1;
     itv.it_value.tv_usec = 0;
     /*设置定时方式*/
     if(-1 == setitimer(ITIMER_REAL, &itv, NULL)){
        perror("error");
        exit(-1);
     }
     /*信号处理*/
     signal(SIGALRM,signal_handler);

     while(1);
     return 0;
}

Guess you like

Origin blog.csdn.net/aaaaaaaa123345/article/details/128898374