Linux C Setitimer Usage notes

Source: Internet
Author: User

In Linux C programming, Setitimer is a relatively common function, can be used to implement the function of time delay and timing, the online have a variety of fragmented usage instructions, have only mentioned the individual usage, today time to practice to collate a more detailed:

header files that need to be introduced when using:

#include <sys/time.h>
Setitimer function Prototypes:

int Setitimer (int which, const struct itimerval *new_value,            struct itimerval *old_value);
Where the which parameter represents the type, the optional values are:

Itimer_real: Calculates the real time of the system, it sends out the SIGALRM signal. itimer_virtual: Calculates the time spent by the process in the user state, and it sends out the SIGVTALRM signal. itimer_prof: It sends out the SIGPROF signal by the time it takes to calculate the process in both the user state and the kernel state. Immediately after the New_value and old_value are itimerval structures, first look at the definition of Itimerval structure:

struct Itimerval {struct timeval it_interval;/* Next value */struct timeval it_value;         /* Current value */};struct timeval {time_t tv_sec;        /* seconds */suseconds_t tv_usec; /* microseconds */};
Itimeval is also composed of two timeval structures, Timeval contains tv_sec and tv_usec two parts, tv_se for seconds, tv_usec for microseconds (i.e. 1/1000000 seconds)

The New_value parameter is used to set the timer, It_interval for the timing interval, it_value for the delay time, the following example shows that after the Setitimer method call succeeds, the delay of 1 microseconds triggers a SIGALRM signal, The SIGALRM signal is triggered every 200 milliseconds thereafter.

SetTimer working mechanism is, first it_value countdown, when the it_value is zero trigger signal, and then reset to It_interval, continue to It_value Countdown, has been so loop down.

Based on this mechanism, the setitimer can be used for both delayed execution and timed execution.

If the It_value is 0 will not trigger the signal, so to be able to trigger the signal, it_value more than 0, if the it_interval is zero, will only delay, not timing (that is, it will only trigger a signal).

The Old_value parameter, usually not used, is set to NULL, which is used to store the New_value value that was set when the last Setitimer call was made.

Here is a simple example of use:

#include <stdio.h> #include <signal.h> #include <sys/time.h>void signalhandler (int signo) {    Switch (signo) {case        sigalrm:            printf ("Caught the Sigalrm signal!\n");            break;   }} int main (int argc, char *argv[]) {    signal (SIGALRM, signalhandler);    struct Itimerval new_value, old_value;    new_value.it_value.tv_sec = 0;    New_value.it_value.tv_usec = 1;    new_value.it_interval.tv_sec = 0;    New_value.it_interval.tv_usec = 200000;    Setitimer (Itimer_real, &new_value, &old_value);        for (;;);         return 0;}

Reference Links:

Http://man7.org/linux/man-pages/man2/setitimer.2.html

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.