The time ftime and gettimeofday System Calls

Processes in User Mode can get the current time and date by means of several system calls:

Returns the number of elapsed seconds since midnight at the start of January 1, 1970 (UTC).

Returns, in a data structure of type timeb, the number of elapsed seconds since midnight of January 1, 1970 (UTC) and the number of elapsed milliseconds in the last second.

gettimeofday( )

Returns, in a data structure named timeval, the number of elapsed seconds since midnight of January 1, 1970 (UTC) (a second data structure named timezone is not currently used).

The former system calls are superseded by gettimeofday( ), but they are still included in Linux for backward compatibility. We don't discuss them further.

The gettimeofday( ) system call is implemented by the sys_gettimeofday( ) function. To compute the current date and time of the day, this function invokes do_gettimeofday( ), which executes the following actions:

1. Disables the interrupts and acquires the xtime_lock read/write spin lock for reading.

2. Gets the number of microseconds elapsed in the last second by using the function whose address is stored in do_gettimeoffset:

If the CPU has a Time Stamp Counter, the do_fast_gettimeoffset( ) function is executed. It reads the TSC register by using the rdtsc assembly language instruction; it then subtracts the value stored in last_tsc_low to obtain the number of CPU cycles elapsed since the last timer interrupt was handled. The function converts that number to microseconds and adds in the delay that elapsed before the activation of the timer interrupt handler (stored in the delay_at_last_interrupt variable mentioned earlier in Section

If the CPU does not have a TSC register, do_gettimeoffset points to the do_slow_gettimeoffset( ) function. It reads the state of the 8254 chip device internal oscillator and then computes the time length elapsed since the last timer interrupt. Using that value and the contents of jiffies, it can derive the number of microseconds elapsed in the last second.

3. Further increases the number of microseconds in order to take into account all timer interrupts whose bottom halves have not yet been executed:

4. Copies the contents of xtime into the user-space buffer specified by the system call parameter tv, adding to the following fields:

5. Releases the xtime_lock spin lock and reenables the interrupts.

6. Checks for an overflow in the microseconds field, adjusting both that field and the second field if necessary:

while (tv->tv_usec >= 1000000) { tv->tv_usec -= 1000000; tv->tv_sec++;

Processes in User Mode with root privilege may modify the current date and time by using either the obsolete stime( ) or the settimeofday( ) system call. The sys_settimeofday( ) function invokes do_settimeofday( ), which executes operations complementary to those of do_gettimeofday( ).

Notice that both system calls modify the value of xtime while leaving the RTC registers unchanged. Therefore, the new time is lost when the system shuts down, unless the user executes the clock program to change the RTC value.

Continue reading here: The adjtimex System Call

Was this article helpful?

0 0