Updated April 5, 2023
Introduction to clock_gettime
The clock_gettime is defined as the function which is provided by POSIX(Portable Operating System Interface) and represented as clock_gettime(), it is called by the system to get the current time of the clock stated by clock_id, that can retrieve the exact time up to nanosecond, the clock is generally in the system that measuring the same time for all processes. It puts into the buffer that pointed by timespec(tp), the tp(timespec) having at least two parameters one is the number of seconds. Another is the number of nanoseconds that expired in the second; this value increases by some multiple nanoseconds based on the system clock’s resolution.
Syntax:
The syntax for clock_gettime is as below:
# include <time.h>
int clock_gettime (clockid_t clockid, struct timespec *tp);
- clock_id:
The clock_id argument is the identifier of the particular clock, and the clock_id has the ID of the clock which we want to get. For example, a clock may be system generated, which is visible to all processes; it measures time. The clock_id allows us to specify the clock which we are using.
These are some Linux supported clock_id’s available:
- CLOCK_REALTIME:
This is a system-generated real-time clock supported by all implementations and the number in second and nanoseconds; this clock is set by clock_settime.
- CLOCK_MONOTONIC:
This clock cannot be set, and it shows the monotonic time. This clock always increases at a constant rate, and it cannot be adjustable.
- CLOCK_PROCESS_CPUTIME_ID:
This is the id that is used to get a high resolution for every process from the CPU.
- CLOCK_THREAD_CPUTIME_ID:
This is a thread processing CPU time clock.
- timespec:
It is a time specification structure that has a pointer where clock_gettime() can store the time. This will be filled in by system calls that contain the value of the clock. The timespec struct has specified in <time.h> directory.
The timespec function has two parameters to set time that are given below-
- tv_sec: is the number of seconds.
- tv_nsec: is the number of nanoseconds that are expired in the second.
How does clock_gettime work?
The clock_gettime() is a function that gives the clock’s current time, which is specified by clock_id. The clock_gettime has a VDSO(Virtual Dynamic Shared Object) implementation on X86 architecture. The VDSO is a shared memory segment between the kernel and the users, allowing the kernel to export functions to be used without being called of the system. It has two arguments; the first one is clock_id, and the second is struct timespec, in which the values can be stored. The struct timespec is a structure that contains again two fields which are tv_sec, for showing time in second and tv_nsec, for displaying time in a nanosecond,
The timespec has its syntax,
#include <time.h>
struct timespec
{
time_t tv_sec;
long tv_nsec;
}
The clock_gettime is related to the time which has specific events in the past. The Linux has differentiated between ‘CLOCK_REALTIME’ and ‘CLOCK_MONOTONIC’ in their references as the ‘CLOCK_REALTIME’ provides the real-time or the time on our watch we can use it in code as ‘clock_gettime (CLOCK_REALTIME, &ts)’.
If we take the number of seconds and we want to convert it into years, while coding this, we will get the elapsed years this will get by calling the function ‘clock_gettime(CLOCK_REALTIME, &ts)’. This means that if we manually change the time or date of our system, it will give the output by ‘clock_gettime(CLOCK_REAL &ts)’, and the time given by the clock is not monotonic means it either never increase or never decrease. The CLOCK_MONOTONIC is a clock_id that does not go back in time even if the clock of our system is changed. It gives the system’s boot time; this is a specific feature by Linux that is not for all POSIX. The time which is returned by ‘clock_gettime(CLOCK_MONOTONIC, &ts)’ is the elapsed time when the system boots. When we want to order a lifetime session, then the CLOCK_MONOTONIC is better, whereas the CLOCK_REALTIME is better when we want absolute time.
Examples of clock_gettime
Given below are the example of clock_gettime:
Example #1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define BILLION 1000000000L;
int main( int argc, char** argv )
{
struct timespec start, stop;
double accum;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 )
{
perror( "clock gettime" );
return EXIT_FAILURE;
}
system( argv[1] );
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 )
{
perror( "clock gettime" );
return EXIT_FAILURE;
}
accum = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec ) / (double)BILLION;
printf( "%lf\n", accum );
return EXIT_SUCCESS;
}
Output:
The above is to calculate the time required to execute the program, which is specified as its first argument the time is printed in seconds; in the above program, we include the directory ‘stdio.h’ which contain the input and output operations ‘printf’ and ‘scanf’ and h is the header file, the directory ‘unistd.h’ provides access to the POSIX operating system,’stdlib.h’ is a standard library which has functions involving memory allocation, process control and conversion and the ‘time.h’ header file contains functions which get and manipulate the information of date and time. Also, we define the directory ‘#define BILLION 1000000000L’, which can calculate the time taken by request struct timespec requestStart. We put condition by using function clock_gettime(),-1 value has been given to set an error it means when the program is success then ‘0’ out will give and if it gives output -1, then there is an error so that -1 is set to get an error.
Example #2
#include <stdio.h>
#include <time.h>
void main(void)
{
time_t now = time(NULL) ;
struct tm tm_now ;
localtime_r(&now, &tm_now) ;
char buff[100] ;
strftime(buff, sizeof(buff), "%Y-%m-%d, time is %H:%M", &tm_now) ;
printf("Time is '%s'\n", buff) ;
}
Output:
Above, we take a simple example about time in clock_gettime; in the main method, we declare it null and assign struct time now by putting that in the buffer, we try to print the time now in the output, out is also given above.
Conclusion
In the above article, we conclude that the clock_gettime() is a function that is recommended to get the current time of the system; under the Linux system, it can be used to get accurate time in nanoseconds, by using struct timespec, we can write small programs, we also conclude that it is time-consuming.
Recommended Articles
We hope that this EDUCBA information on “clock_gettime” was beneficial to you. You can view EDUCBA’s recommended articles for more information.