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.
We all know that the tar command under Linux system can be used to decompress files
When the Linux running level is set to 6, there will be an infinite restart problem
In the Linux system, the files can be decompressed on the Deepin command line. You can also decompre
Raid is divided into software Raid and hardware Raid. Because the hardware Raid is
How Linux uses the at command to perform tasks
How to use the vmstat command in Linux
Summary of usage of Linux chmod commands
How to solve the Linux system Tail command prompt No space left on device problem
How to upgrade OpenSSH under Linux system?
What should I do if there is an 807 error after Linux establishes a VPN?
The implementation principle of Linux system TSO
Linux how to use vim for advanced editing
What should I do if the Windows file shows garbled characters on Ubuntu?
Can the win7 computer's boot record function not be used?
The difference between a common server cabinet and an advanced server cabinet
Windows7×64 blue screen 0×00000101(0×0000000000000031, 0×0000000000000
Win8 "wermgr.exe - Application Error" error
There win7 under what recipe can let the computer do a good sleep (4)
Win7 system input method icon is gone, how to do
Win7 system settings window border and taskbar color method graphic tutorial
Windows NT 4.0 upgrade to win 2003 related issues
Windows10 multi-account switch login method steps
Win 7 pseudo-skills "Maximum memory" don't take it seriously