Getting the Current Time

The current time of the system needs to be known for two reasons: First, many operations rely on time stamps — for instance, the kernel needs to record when a file was last changed or when some log information was produced. Second, the absolute time — that is, the real time of the outside world — of the system is needed to inform the user with a clock, for example.

While absolute accuracy is not too important for the first purpose as long as the time flow is continuous (i.e., the time stamps of successive operations should follow their order), it is more essential for the second purpose. Hardware clocks are notorious for being either fast, slow, or a random combination of both. There are various methods to solve this problem, with the most common one in the age of networked computers being synchronization with a reliable time source (e.g., an atomic clock) via NTP. Since this is purely a userland issue, I won't discuss it any further.

Two means are provided to obtain timing information:

1. The system call adjtimex. A small utility program of the same name can be used to quickly display the exported information. The system call allows for reading the current kernel internal time. Other possibilities are documented in the associated manual page 2(adjtimex).

2. The device special file /dev/rtc. This source can be operated in various modes, but one of them delivers the current date and time to the caller.

I focus on adjtimex in the following. The entry point is as usual sys_adjtimex, but after some preparations, the real work is delegated to do_adjtimex. The function is rather lengthy, but the portion required for our purposes is quite compact:

kernel/time.c int do_adjtimex(struct timex *txc) {

do_gettimeofday(&txc->time);

The call to do_gettimeofday obtains the kernel's internal time in the best possible resolution. The best time source that was selected by the kernel as described in Section 15.4 is used for this purpose.

Continue reading here: Managing Process Times

Was this article helpful?

0 0