Time Service Functions in Unix/Linux

In almost every operating system (OS), the OS kernel provides a variety of time related services. Time services can be invoked by system calls, library functions and user level commands. In this section, we shall cover some of the basic time service functions of Linux.

1. Gettimeofday-Settimeofday

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

int settimeofday(const struct timeval *tv, const struct timezone *tz);

These are system calls to the Linux kernel. The first parameter, tv, points to a timeval structure.

struct timeval {

time_t          tv_sec;         /* seconds */

suseconds_t     tv_usec;        /* microseconds */

};

The second parameter, timezone, is obsolete and should be set to NULL. The gettimeofday() function returns the current time in seconds and microseconds of the current second. The settimeofday() function sets the current time. In Unix/Linux, time is represented by the number of seconds elapsed since 00:00:00 of January 1, 1970. It can be converted to calendar form by the library function ctime (&time). The following examples demonstrate gettimeofday() and settimeofday().

(1). Gettimeofday system call

Example 5.1 Get system time by gettimeofday()

/********* gettimeofday.c file *********/

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

struct timeval t;

int main()

{

gettimeofday(&t, NULL);

printf(“sec=%ld usec=%d\n”, t.tv_sec, t.tv_usec);

printf((char *)ctime(&t.tv_sec));

}

The program should display the current time in seconds, microseconds and also the current date and time in calendar form, as in

sec=1515624303 usec=860772

Wed Jan 10 14:45:03 2018

(2). Settimeofday system call

Example 5.2 Set system time by settimeofday()

/********* settimeofday.c file *********/

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

#include <time.h>

struct timeval t;

int main()

{

int r;

t.tv_sec = 123456789;

t.tv_usec= 0;

r = settimeofday(&t, NULL);

if (!r){

printf(“settimeofday() failed\n”);

exit(1);

}

gettimeofday(&t, NULL);

printf(“sec=%ld usec=%ld\n”, t.tv_sec, t.tv_usec);

printf(“%s”, ctime(&t.tv_sec)); // show time in calendar form

}

The output of the program should show something like

sec=123456789 usec=862

Thu Nov 29 13:33:09 1973

Based on the printed date and year (1973), it seems that the settimeofday() operation has succeeded. However, in some Linux systems, e.g. Ubuntu 15.10, the effect may only be temporary. If the reader runs the gettimeofday program again, it will show that Linux has changed the system time back to the correct real time. This shows that the Linux kernel has the ability to use the real-time clock (and other time synchronization protocols) to correct any deviations of system time from real time.

2. The Time System Call

Example 5.3 The time system call

time_t time(time_t *t)

returns the current time in seconds. If the parameter t is not NULL, it also stores the time in the memory pointed by t. The limitation of the time system call is that it only provides resolutions in seconds, not in microseconds. This example shows how to get system time in seconds.

/************ time.c file ***********/

#include <stdio.h>

#include <time.h>

time_t start, end;

int main()

{

int i;

start = time(NULL);

printf(“start=%ld\n”, start);

for (i=0; i<123456789; i++); // delay to simulate computation

end = time(NULL);

printf(“end =%ld time=%ld\n”, end, end-start);

}

The output should print the start time, end time and the number of seconds from start to end.

3. The Times System Call

The times system call

clock_t times(struct tms *buf);

can be used to get detailed execution time of a process. It stores the process time in a struct tms buf, which is

struct tms{

clock_t tms_utime;  // user mode time
clock_t tms_stime;  // system mode time
clock_t tms_cutime;  // user time of children
clock_t tms_cstime;  // system time of children

};

All times reported are in clock ticks. This provides information for profiling an executing process, including the time of its children processes, if any.

4. Time and Date Commands

date: print or set the system date and time

time: report process execution time in user mode, system mode and total time

hwclock: query and set the hardware clock (RTC), can also be done through BIOS.

Source: Wang K.C. (2018), Systems Programming in Unix/Linux, Springer; 1st ed. 2018 edition.

Leave a Reply

Your email address will not be published. Required fields are marked *