官术网_书友最值得收藏!

Time

Some readers might find it surprising to know that even performing simple tasks such as getting the current date and time require system calls to ask the operating system for this information. Even to this day, a dedicated chip (with a battery, in case of loss of power) is provided on the system to maintain the current date and time.

If this information is needed, a system call must be made to request it. When this happens, the operating system will ask the device driver responsible for managing the chip what date and time it is currently storing, and then this information will be returned to the application. 

It should be noted that not all time interfaces require system calls. For example, most high-resolution timers, which are designed to compare a high-resolution number before and after an operation has taken place, do not need the operating system to perform this action. This is because these high-resolution timers usually exist directly in the CPU, and their values can be extracted using a simple instruction.

The downside to these types of timers is that their values in and of themselves are usually meaningless (that is, the difference between the values returned is what provides meaning, not the values themselves). Essentially, these timers are usually nothing more than a counter that increments each time the CPU ticks (that is, executes an instruction).

Since modern CPUs can dynamically change their frequency, the values these counters store depends on how long the CPU has executed since the previous power cycle, and at what frequency the CPU was set while it was executing.

There isn't even a guarantee that the value in one counter will be the same as the value read in another counter on another physical core, as each physical core is capable of changing its own frequency independently of other cores on multi-core CPUs. 

The benefit of high-resolution timers is that they can be executed extremely quickly (as you are just executing an instruction that reads a counter in the CPU). The difference between two measured values can be used to carry out tasks such as measuring how long it takes to execute small functionsa task that usually doesn't work with standard timers, as they don't have enough granularity.

In Chapter 11, Time Interfaces in Unix, we will go over these details and even provide an example of how to do this yourself. 

主站蜘蛛池模板: 荣昌县| 亳州市| 平罗县| 马尔康县| 寻甸| 长汀县| 凌云县| 长顺县| 丰镇市| 确山县| 叙永县| 灵川县| 永顺县| 威远县| 屏山县| 巫山县| 漯河市| 抚顺县| 中宁县| 望城县| 齐齐哈尔市| 宁远县| 枝江市| 永和县| 乐安县| 衡水市| 玉林市| 松江区| 南郑县| 延寿县| 微博| 尚志市| 华宁县| 敦化市| 新龙县| 康保县| 郑州市| 梁河县| 洛浦县| 汉中市| 方山县|