Timer timer is widely used. In Linux, there are several methods:
1. Use sleep () and usleep ()
Sleep precision is 1 second, usleep precision is 1 subtle, specificCodeIt will not be written. The disadvantages of using this method are obvious. in Linux, sleep functions cannot guarantee accuracy, especially when the system load is large, sleep usually times out.
2. Use semaphores sigalrm + alarm ()
The precision of this method can reach 1 second, and the * nix system's semaphore mechanism is used. First, register the signal sigalrm processing function, call alarm (), and set the timer 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 ;}
Alarm Method is good, but it cannot be less than 1 second precision first.
3. Use the RTC Mechanism
The RTC mechanism uses the real time clock mechanism provided by the system hardware to read RTC Hardware/dev/RTC and set the RTC frequency through IOCTL (). The Code is 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 <100; 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 method is convenient. It uses the RTC provided by the system hardware, and the accuracy is adjustable and very high.
4. Select ()
This method is seen when reading apue books. The method is relatively unpopular and you can use select () to set the timer. The principle is to use the 5th parameters of select () method, the first parameter is set to 0, all three file descriptor sets are set to null, and the second parameter is a time struct. 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, & temp); printf ("timer \ n"); return;} int main () {int I; for (I = 0; I <100; I ++) settimer (1, 0); Return 0 ;}
The precision of this method can reach a subtle level. There are many select ()-based multi-thread timers on the Internet, which shows that the select () stability is still very good.
Summary: If you have low requirements on the system, you can consider using simple sleep (). After all, a line of code can solve the problem. If the system has high requirements on accuracy, the RTC and select () mechanisms can be considered.