Introduction
After familiar with the basic programming method, our interest lies in how does the computer achieve all this? What does the operating system and the library function do between the application-layer APIs and the underlying system hardware?
First look at the general process of Linux time processing:
Figure 1. Time processing process
The application section has been covered in detail in the first section, and in the second part I will introduce some general overviews of hardware and GlibC related implementations.
Hardware
The most common clock hardware in a PC is the following.
RTC (Real time Clock, live clock)
When people need to know the time, they can look at the clocks. The hardware of a clock in a computer system is an external clock. It relies on the battery on the motherboard to maintain the accuracy of the clock when the system is powered down. When the computer needs to know the time, it needs to read the clock.
In the x86 system, this clock is generally called real time Clock. The RTC is a CMOS chip on the motherboard, such as Motorola 146818, which is independent of the CPU and other chips and can operate the RTC through the 0x70 and 0x71 ports. The RTC can periodically trigger interrupts on IRQ 8, but with low precision, from 2HZ to 8192HZ.
In the case of Motorola 146818, the software can read and write the following values through the I/O instruction:
Figure 2. Motorola 146818
As you can see, the RTC can provide real-time time values that are accurate to seconds.