A timer under Linux

Source: Internet
Author: User

After 1.alarm () runs, the process will continue to run. During the later (alarm) run, the signal sigalrm is received after seconds seconds and its handler function is run.

#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 timers, but only accurate to seconds, but we assume that 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: Calculated as the real time of the system. It sends out a SIGALRM signal.
Itimer_virtual:-Calculates the time that the process spends in the user state. It sends out a SIGVTALRM signal.
ITIMER_PROF: Calculated based on the time taken by the process in both the user and kernel states. It sends out a SIGPROF signal.
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 successfully returned 0. otherwise returns-1.

Here is a simple demonstration of the Setitimer call, in which a sigalrm is emitted every second. Send a SIGVTALRM signal every 0.5 seconds: [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 (;;) ;}
The Setitimer does not cause the thread to jam, nor causes the thread to switch the action, simply starts a timer, starts the timing. And such timing should be based on the kernel. (Windwos's SetTimer is a model based on a message). Setitimer Although there are three types of itimer_real,itimer_virtual itimer_prof, the same process at the same time. One type can only have 1 setitimer.


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.