A timer under Linux

Source: Internet
Author: User

After 1.alarm () execution, 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 <stdio.h> #include <unistd.h> #include <signal.h>void sigalrm_fn (int sig) {    printf (" Alarm!\n ");    Alarm (2);    return;} int main (void) {    signal (SIGALRM, SIGALRM_FN);    Alarm (1);    while (1) Pause ();}

2.alarm timer, but only accurate to seconds, but if we need to use a more accurate timer can be used 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: [code=c/c++]

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <            time.h> #include <sys/time.h>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;    SEC = 5;    printf ("Process ID is%d\n", getpid ());    Signal (SIGALRM, sigroutine);    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);    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 (;;) ;}
Setitimer does not cause the thread to block or cause the thread to switch, it is simple to start a timer, start timing, and this timing should be based on the kernel, (Windwos SetTimer is based on a message model) Setitimer Although there are three types itimer_real,itimer_virtual itimer_prof, but at the same time the same process, one type can only have 1 setitimer.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A timer under Linux

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.