


Timer Timer's various application methods and advantages and disadvantages under 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; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

This guide explores various methods for comparing text files in Linux, a crucial task for system administrators and developers. We'll cover command-line tools and visual diff tools, highlighting their strengths and appropriate use cases. Let's assum

The main difference between Linux and Windows in user account management is the permission model and management tools. Linux uses Unix-based permissions models and command-line tools (such as useradd, usermod, userdel), while Windows uses its own security model and graphical user interface (GUI) management tools.
