Linux C setitimer Usage instructions __linux

Source: Internet
Author: User
Tags function prototype

Turn from: http://blog.csdn.net/lixianlin/article/details/25604779


In Linux C programming, Setitimer is a relatively common function, can be used to realize the delay and timing of the function, the Internet has a variety of sporadic usage instructions, have only mentioned the individual usage, today, the practice of sorting out a more detailed:

Header files to be introduced when using:[CPP]View plain copy #include <sys/time.h> setitimer function prototype:[CPP]View plain copy int setitimer (int which, const struct itimerval *new_value, struct itimerval); Where the which parameter represents the type, the optional values are:Itimer_real: In the real time of the system, it sends out the SIGALRM signal.itimer_virtual: The process is calculated by the time it takes in the user state to send a SIGVTALRM signal.itimer_prof: The process in the user state and the kernel state of the time to calculate, it sends out the SIGPROF signal. followed by the New_value and Old_value are itimerval structures, first look at the definition of the ITIMERVAL structure:[CPP]View plain copy struct Itimerval {struct timeval it_interval;/* Next value */struct timeval;      /* 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, where tv_se is seconds and tv_usec is microseconds (i.e. 1/1000000 seconds).

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

SetTimer work mechanism is, first to It_value Countdown, when the it_value for zero trigger signal, and then reset to It_interval, continue to the It_value countdown, has been such a cycle down.

Based on this mechanism, Setitimer can be used both for delay execution and timed execution.

if the It_value is 0 does not trigger the signal, so to be able to trigger the signal, It_value is greater than 0, if the it_interval is zero, will only delay, will not be timed (that is, only one signal will be triggered).

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

Here is a simple example of use:
[CPP] View plain copy #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;  }  

Related Article

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.