Linux setitimer function operation skills

  

Linux system use setitimer function can be high-precision timing function, used to execute functions regularly, the following small series will give you a detailed introduction to the use of the setitimer function, let's understand it. .

Description: setitimer a process can only have one before the next timing will be covered by a more timers can only think of a process to achieve their own.

setitimer() does not support multiple simultaneous uses in the same process to support multiple timers.

The linux-related timer (setitimer) is described as follows:

The Linux system provides three timers for each process, each of which counts in its own different domain. When any timer counts up, the system sends a signal to the process and the counter is reset.

A total of the following three counter forms are supported:

ITIMER_REAL The counter is decremented by 1 in real time, and then the SIGALRM signal is sent after the count is compared.

ITIMER_VIRTUAL When the process counts during execution, then the SIGVTALRM signal is sent to the process when the count is complete.

ITIMER_PROF counts when the process is executed and the system is executing on behalf of the process

[getitimer/setitimer system call]

Description:

Get or set the value of the interval timer. The system provides three types of timers for the process, each class decrementing its value in a different time domain. When the timer expires, the signal is sent to the process, after which the timer restarts.

Usage:

#include "sys/time.h"

int getitimer(int which, struct itimerval *value);

int setitimer( Int which, const struct itimerval *value, struct itimerval *ovalue);

Parameters:

which: Intermittent timer type, there are three options

ITIMER_REAL //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 during process and system execution, and the signal sent is SIGPROF.

value,ovalue: time parameter, the prototype is as follows

struct itimerval

{

struct timeval it_interval;

struct timeval it_value;

};

struct timeval

{

long tv_sec;

long tv_usec;

};< Br>

getitimer() fills in the structure pointed to by value with the current value of the timer.

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

Return Description:

Returns 0 when executed successfully. Failed to return -1, errno is set to one of the following values

EFAULT:value or ovalue is a pointer that is not valid

EINVAL: its value is not one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF

#include "stdio.h"

#include "unistd.h"

#include "signal.h"

#include "string.h"

#include "sys/time.h"

#include "errno.h"

void PrintMsg(int Num)

{

printf(“%s/n”, “Hello World”);

return;

}

int main(int argc, char* argv [])

{

signal(SIGALRM, PrintMsg);

struct itimerval tick;

tick.it_value.tv_sec = 10; //10 The timer will be started after a second

tick.it_value.tv_usec = 0;

tick.it_interval.tv_sec =1; //After the timer starts, every 1 second will execute the corresponding Function

tick.it_interval.tv_usec = 0;

//setitimer will trigger SIGALRM signal

int ret = setitimer(ITIMER_REAL, &tick, NULL);

if ( ret != 0)

{

printf(“Set timer error. %s /n”, strerror(errno) );

return -1;

}

printf(“ Wait!/n”);

getchar();

return 0;

}

The above is the usage of the setitimer function in Linux. The setitimer function has three types of timer selections. In order to achieve higher precision timing, it is better to choose the setitimer function.

Copyright © Windows knowledge All Rights Reserved