Linux setitimer is a commonly used software for computer players. Then I will study and study Linux setitimer in depth. Here I will discuss with you how to use Linux setimer, hoping to help you. Linux setitimer () is a Linux API, not the Standard Library of C language. Linux setitimer () has two functions. One is to execute a function after a specified period of time, the second is to execute a function in each cell for a period of time. The following program demo uses Linux setitimer ().
- view plaincopy to clipboardprint?/*
- Filename : timer.cpp
- Compiler : gcc 4.1.0 on Fedora Core 5
- Description : Linux setitimer() set the interval to run function
- Synopsis : #include <sys/time.h>
- int Linux setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
- struct itimerval {
- struct timerval it_interval;
- struct timerval it_value;
- };
- struct timeval {
- long tv_sec;
- long tv_usec;
- }
- Release : 11/25/2006
- */
- #include <stdio.h> // for printf()
- #include <unistd.h> // for pause()
- #include <signal.h> // for signal()
- #include <string.h> // for memset()
- #include <sys/time.h> // struct itimeral. Linux setitimer()
- void printMsg(int);
- int main() {
- // Get system call result to determine successful or failed
- int res = 0;
- // Register printMsg to SIGALRM
- signal(SIGALRM, printMsg);
- struct itimerval tick;
- // Initialize struct
- memset(&tick, 0, sizeof(tick));
- // Timeout to run function first time
- tick.it_value.tv_sec = 1; // sec
- tick.it_value.tv_usec = 0; // micro sec.
- // Interval time to run function
- tick.it_interval.tv_sec = 1;
- tick.it_interval.tv_usec = 0;
- // Set timer, ITIMER_REAL : real-time to decrease timer,
- // send SIGALRM when timeout
- res = Linux setitimer(ITIMER_REAL, &tick, NULL);
- if (res) {
- printf("Set timer failed!!\n");
- }
- // Always sleep to catch SIGALRM signal
- while(1) {
- pause();
- }
- return 0;
- }
- void printMsg(int num) {
- printf("%s","Hello World!!\n");
- }
-
When the time for Linux setitimer () to execute the timer is reached, it will call SIGALRM signal. Therefore, in line 30th, the function to be executed by signal () is specified to SIGALRM. In line 3, call Linux setitimer () to set timer, but Linux setitimer ()
The second parameter is sturct, which is used to set the timeout time. Therefore, this struct is set for rows 36th to 40th. Itimerval. it_value sets the number of seconds after the first function execution. itimerval. it_interval sets the number of seconds after the function execution. If you only want to delay the function execution for a period of time, you only need to set itimerval. it_value.
If you want to set the interval to execute the function for a period of time, it_value and it_interval must be set. Otherwise, the function cannot be executed for the first time. The TV _sec values for rows 36th and 39th are sec, 37th and 40 Act micro sec (0.001 sec ). ITIMER_REAL, the first parameter of row 43rd, indicates that timer is reduced in real-time mode, and SIGALRM signal is sent out in timeout.
The third parameter stores the old timeout value. If this parameter is not required, specify NULL. The command system enters the sleep state for pause () of Line 1. Wait for any signal and use the while (1) infinite loop to execute pause (), in this way, you can always receive SIGALRM signal to execute the function at intervals. If while (1) is removed, the function will only be executed once.
- Common LinuxYUM commands
- A Brief Introduction to the use of Linux Operators
- One of the methods to modify the default Linux Startup System
- Install cross compiler in Linux SkyEye
- Linux rpm installation source code Packaging