Setitimer () Timing Function

Source: Internet
Author: User

Setitimer () is a Linux API, not the standard library of C language.

Setitimer () has two functions: one is to execute a function after a period of time, and the other is to execute a function for a period of time.

The following program demo shows how to use setitimer ().

View plaincopy to clipboardprint?
/*
 

 
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 ");

}

/*

 

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 () is a more accurate timer and is a Linux API

The following code is an example of Countdown:

View plaincopy to clipboardprint?
# Include <signal. h>
# Include <sys/time. h>
Static int time_count = 30;
Static void sigalrm_handler (INT sig)
{
Time_count --;
Printf ("timer signal .. % d/N", time_count );
}
Struct itimerval V; // timer struct
Time_count = 10; // initialize again
Signal (sigalrm, sigalrm_handler); // bind a signal function
V. it_value. TV _sec = 1;
V. it_value. TV _usec = 0;
V. it_interval. TV _sec = 1; // interval: 1 s
V. it_interval. TV _usec = 0;
Setitimer (itimer_real, & V, null); // structure association, start
...
...
// ============ End this timer =================================
V. it_value. TV _sec = 0;
V. it_value. TV _usec = 0;
V. it_interval. TV _sec = 0;
V. it_interval. TV _usec = 0;
Setitimer (itimer_real, & V, null );

# Include <signal. h>
# Include <sys/time. h>
Static int time_count = 30;
Static void sigalrm_handler (INT sig)
{
Time_count --;
Printf ("timer signal .. % d/N", time_count );
}
Struct itimerval V; // timer struct
Time_count = 10; // initialize again
Signal (sigalrm, sigalrm_handler); // bind a signal function
V. it_value. TV _sec = 1;
V. it_value. TV _usec = 0;
V. it_interval. TV _sec = 1; // interval: 1 s
V. it_interval. TV _usec = 0;
Setitimer (itimer_real, & V, null); // structure association, start
...
...
// ============ End this timer =================================
V. it_value. TV _sec = 0;
V. it_value. TV _usec = 0;
V. it_interval. TV _sec = 0;
V. it_interval. TV _usec = 0;
Setitimer (itimer_real, & V, null );

The principle is that a sigalrm signal is sent once every 1 S. If the value is set to zero, the signal is disabled.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ling1874/archive/2009/09/28/4603696.aspx

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fanwenbo/archive/2008/07/13/2645362.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.