Home  >  Article  >  System Tutorial  >  Timer Timer's various application methods and advantages and disadvantages under Linux

Timer Timer's various application methods and advantages and disadvantages under Linux

WBOY
WBOYOriginal
2024-07-19 15:52:09906browse

定时器 Timer 在 Linux 下的多种应用方法及优缺点

Timer application scenarios are very wide. Under Linux linux application timer, there are the following ways:

1 Hongqi Linux desktop version, using sleep() and usleep()

The accuracy of sleep is one second, and the accuracy of usleep is 1 microsecond. The specific code will not be written. The disadvantages of using these techniques are obvious. In Linux systems, sleep functions cannot guarantee accuracy. Especially when the system load is relatively heavy, sleep usually times out.

2. Use the semaphore SIGALRM+alarm()

The accuracy of these methods can reach one second. They use the semaphore mechanism of the *nix system. First, register the semaphore SIGALRM processing function, call alarm(), and set the timing width. The code is as follows:

#include 
#include 
void timer(int sig)
{
if(SIGALRM == sig)
{
printf("timern");
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;
}

应用定时器设计电子钟_应用定时器2安卓版下载_linux 应用定时器

Although the alarm method is very good, it is difficult to achieve an accuracy higher than one second first.

3. Use RTC mechanism

The RTC mechanism uses the RealTimeClock mechanism provided by the system hardware, reads the RTC hardware /dev/rtc, and sets the RTC frequency through ioctl(). The code is as follows:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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("timern");
}
/* Disable periodic interrupts */
ioctl(fd, RTC_PIE_OFF, 0);
close(fd);
return 0;
}

These methods are relatively convenient and use the RTC provided by the system hardware. The accuracy is adjustable and extremely high.

4, use select()

I heard these methods when reading the APUE magic book, and the skills are relatively niche Linux application timer Linux common commands, use select() to set the timer; the principle is based on the 5th parameter of the select() method , the first parameter is set to 0, the three file descriptor sets are all set to NULL, the fifth parameter is the time structure, the code is as follows:

#include 
#include 
#include 
#include 
/*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("timern");
return ;
}
int main()
{
int i;
for(i = 0 ; i < 100; i++)
setTimer(1, 0);
return 0;
}

The accuracy of these techniques can reach a subtle level. There are many multi-threaded timers based on select() on the Internet, which shows that the stability of select() is still very good.

Summary: If the system requirements are relatively low, you can consider using a simple sleep(), although one line of code can solve it; if the system has relatively high accuracy requirements, you can consider the RTC mechanism and select() mechanism.

The above is the detailed content of Timer Timer's various application methods and advantages and disadvantages under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn