Setitimer () is a Linux API, not the standard library of C language. 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 setitimer ().
[CSHARP]View plaincopyprint?
- /*
-
-
- Filename: timer. cpp
-
- Compiler: GCC 4.1.0 on Fedora Core 5
-
- Description: setitimer () set the interval to run Function
-
- Synopsis: # include <sys/time. h>
-
- Int 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. 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 = 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 setitimer () to be executed is reached, sigalrm signal is called. Therefore, in line 30th, the function to be executed by signal () is specified to sigalrm. Set the timer in the second line of the call setitimer (), but the second parameter of setitimer () is sturct, which is used to set the timeout time. Therefore, the struct is set for the second line to the second line. Itimerval. it_value sets the number of seconds after the first function execution, itimerval. it_interval sets the number of seconds after which the function will be executed. If you only want to delay the execution of the function 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.
Setitimer ()