5.5. System Calls Related to Timing Measurements

Several system calls allow User Mode processes to read and modify the time and date and to create timers. Let us briefly review them and discuss how the kernel handles them.

5.5.1. The time( ), ftime( ), and gettimeofday( ) System Calls

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

time( )

Returns the number of elapsed seconds since midnight at the start of January 1, 1970

ftime( )

Returns, in a data structure of type timeb, the number of elapsed seconds since midnight of January 1, 1970; the number of elapsed milliseconds in the last second; the time zone; and the current status of daylight saving time

gettimeofday( )

Returns the same information as ftime( ) in two data structures named timeval and timezone

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. In order to compute the current date and time of the day, this function invokes do_ gettimeofday( ), which executes the following actions:

  • Copies the contents of xtime into the user-space buffer specified by the system call parameter tv:

    *tv = xtime;
  • Updates the number of microseconds by invoking the function addressed by the do_ gettimeoffset variable:

    tv->tv_usec += do_gettimeoffset(  );

    If the CPU has a Time Stamp Counter, the do_fast_gettimeoffset( ...

Get Understanding the Linux Kernel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.