Timers under Linux: Alarm () and Setitimer ()

Source: Internet
Author: User

There are two types of timers under Linux, each of which is described below:

1, Alarm

Using alarm () and signal () is sufficient if it is not required to be precise.

unsigned int alarm (unsigned int seconds)

Function Description: Alarm () is used to set the signal SIGALRM to the current process after the number of seconds specified by the parameter seconds. If the parameter seconds is 0, the previously set alarm is canceled and the remaining time is returned.

Return value: Returns the number of seconds left before the alarm, or 0 if no alarm was previously set.

After alarm () executes, the process will continue to execute and will receive a signal sigalrm after seconds seconds and execute its handler function during the execution of the post (alarm).

#include #include #include void sigalrm_fn (int sig) {printf ("alarm!/n"); alarm (2); return;} int main (void) {signal (SIGALRM, SIGALRM_FN); alarm (1); while (1) Pause ();}

2, Setitimer ()

int Setitimer (int which, const struct itimerval *value, struct itimerval *ovalue));

Setitimer () is powerful than alarm and supports 3 types of timers:

Itimer_real: It takes the real time of the system to calculate, 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 with the time that the process takes in both the user state and the kernel state.

Setitimer () The first parameter which specifies the timer type (one of the three above); The second parameter is an instance of the structure Itimerval; The third parameter is not processed.

The Setitimer () call returns 0 successfully, otherwise returns-1.

Here is a simple demonstration of the Setitimer call, in which a sigalrm is emitted every 0.5 seconds and a SIGVTALRM signal is emitted:

 #include #include #include #include #include int sec;void sigroutine (int signo) { Switch (signo) {case sigalrm:printf ("Catch a Signal-sigalrm/n"); Signal (SIGALRM, sigroutine); Break;case SIGVTALRM: printf ("Catch a Signal-sigvtalrm/n"); Signal (SIGVTALRM, sigroutine); break;} return;} int main () {struct Itimerval value, Ovalue, value2;//(1) sec = 5;printf ("Process ID is%d/n", getpid ()); Signal (SIGALRM, SIG routine); Signal (SIGVTALRM, sigroutine); value.it_value.tv_sec = 1;value.it_value.tv_usec = 0;value.it_interval.tv_ SEC = 1;value.it_interval.tv_usec = 0;setitimer (Itimer_real, &value, &ovalue); (2) value2.it_value.tv_sec = 0;value2.it_value.tv_usec = 500000;value2.it_interval.tv_sec = 0;value2.it_interval.tv
  _usec = 500000;setitimer (itimer_virtual, &value2, &ovalue); for (;;);} 

(1) struct itimerval

struct Itimerval {struct timeval it_interval;/* Timer interval */struct timeval it_value;/* Current value */};itimerval: ---Intervalval--value

The it_value in the itimerval structure is the reduced time, and when this value is 0, the corresponding signal is sent out. Then set the It_value to the It_interval value.

(2) Setitimer ()

Setitimer () sets a timer for the process in which it is located, and if Itimerval.it_interval is not 0 (0 of the two fields in It_interval), the timer will continue to be active (a signal is sent every time)

Note: The Linux signaling mechanism is basically inherited from Unix systems. The signaling mechanism in the early Unix system was relatively simple and primitive, and later exposed some problems in practice, so the signals based on the early mechanism were called "unreliable signals" and the signal value was less than Sigrtmin (sigrtmin=32,sigrtmax=63). Signals are unreliable. This is the source of "unreliable signals". The main problem is that the response to the signal is set to the default action each time the signal is processed by the process. In some cases, it will cause error handling of the signal, so if the user does not want to do so, it is necessary to re-install the signal by calling signal () at the end of the signal processing function.

Timers under Linux: Alarm () and Setitimer ()

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.