Summary of several methods of implementing timer timer under Linux _linux

Source: Internet
Author: User
Tags semaphore usleep

Timer Timer application Scenarios are very wide, under Linux, there are several ways:

1, using sleep () and Usleep ()

Sleep accuracy is 1 seconds, usleep precision is 1 subtle, the specific code is not written. The disadvantage of using this method is obvious, in Linux system, the Sleep class function can not guarantee the accuracy, especially in the system load is relatively large, sleep will generally have timeout phenomenon.

2, using signal SIGALRM + alarm ()

This method of precision can reach 1 seconds, which utilizes the *nix system's semaphore mechanism, first registers the semaphore SIGALRM processing function, invokes alarm (), sets the timing length, the code is as follows:

#include <stdio.h>
#include <signal.h>

void timer (int sig)
{
    if (sigalrm = = sig)
    {
        printf ("timer\n");
        Alarm (1);    We Contimue set the timer
    } return

    ;

int main ()
{
    signal (SIGALRM, timer);//relate the signal and function

    Alarm (1);    Trigger the timer

    GetChar ();

    return 0;
}

The alarm method is good, but it cannot first be less than 1 seconds of precision.

3, using the RTC mechanism

The

RTC mechanism uses the real time clock mechanism provided by the system hardware to set the RTC frequency by reading the RTC Hardware/DEV/RTC through IOCTL (), as follows:

#include <stdio.h> #include <linux/rtc.h> #include <sys/ioctl.h> #include <sys/time.h> # Include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <
    stdlib.h> int main (int argc, char* argv[]) {unsigned long i = 0;
    unsigned Long data = 0;
    int retval = 0;

    int fd = open ("/DEV/RTC", o_rdonly);
        if (FD < 0) {perror ("open");
    Exit (errno);
        }/*set the Freq as 4hz*/if (IOCTL (FD, Rtc_irqp_set, 1) < 0) {perror ("IOCTL (Rtc_irqp_set)");
        Close (FD);
    Exit (errno);
        }/* Enable periodic interrupts/if (IOCTL (FD, rtc_pie_on, 0) < 0) {perror ("IOCTL (rtc_pie_on)");
        Close (FD);
    Exit (errno);
            for (i = 0; i < i++) {if Read (FD, &data, sizeof (unsigned long) < 0) {
            Perror ("read");
            Close (FD);

        Exit (errno);
    }    printf ("timer\n");
    }/* Disable periodic interrupts * * IOCTL (FD, Rtc_pie_off, 0);

    Close (FD);
return 0;
 }

This approach is more convenient, using the system hardware provided by the RTC, precision adjustable, and very high.

4, using Select ()

This method is seen in the Apue of God's book, method is relatively unpopular, by using select (), to set the timer; The principle utilizes the 5th parameter of the Select () method, the first parameter is set to 0, the three file descriptor sets are set to NULL, and the 5th parameter is the time structure, and the code is as follows:

#include <sys/time.h>
#include <sys/select.h>
#include <time.h>
#include <stdio.h >

/*seconds:the seconds mseconds:the micro seconds*/
void SetTimer (int seconds, int mseconds)
{
    struct Timeval temp;

    temp.tv_sec = seconds;
    Temp.tv_usec = mseconds;

    Select (0, NULL, NULL, NULL, &TEMP);
    printf ("timer\n");

    return;
}

int main ()
{
    int i;

    for (i = 0; i < i++)
        SetTimer (1, 0);

    return 0;
}

The accuracy of this method can be subtle, and there are many multi-threaded timers based on select () on the Web, indicating that select () stability is very good.

Summary: If the system requirements are relatively low, you can consider the use of simple sleep (), after all, a line of code can be resolved, if the system for precision requirements are higher, you can consider the RTC mechanism and select () mechanism.

The above is a small series of Linux to bring to the realization of timer timer Several methods of summing up all the content, I hope that we support cloud-Habitat Community ~

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.