C Programming

clock_gettime C Function

“The clock gettime () function retrieves the time currently shown by the clock identified by clock id and writes it to the buffer. The “clock_gettime ()” function will return 0 or -1 depending on whether they are successful. With up to nano accuracy, the POSIX function “clock_gettime ()” function can receive the time from a wide range of different clocks. Clocks can be per-processor or per-thread, monitoring the CPU time used by a specific process, or system-wide, evaluating the same amount of time for all operations.

In this guide, we will go through this “clock_gettime ()” function in deep detail. We will also perform examples in which we will utilize the “clock_gettime ()” function in C programming.”

Syntax

int clock_gettime (clockid_t clock_id, struct timespec *tp);

Example # 1

The demonstration of the examples in this guide will be carried out using Ubuntu 20.04. Installing the GCC compiler comes first, after which we launch the Ubuntu 20.04 text editor and enter some code. In this text editor, we are utilizing some header files at the start of the C code here. Whenever we have to write the C code, we just need to add the header files. Without these files, we will not get the functions that we need here in our C code.

Here, you can see that we put the “stdio.h” header file at the first line of the code, which is the standard input and output header file. Then we place the “sys/time. h” and the “time. h” header file. We added this header file, so we will use the “clock_gettime ()” function in this code. Then we add the “main ()” function here and declare it as an “int” data type. We pass two parameters for this “int main ()” function. The first parameter is “int argc” and the second parameter is “char **argv” here for the “main ()” function. We declare “time” as the “struct timespec” inside this main () function.

Below this, we utilize the “clock_gettime ()” function, and we have two parameters for the “clock_gettime ()” function. The clock_gettime supports CLOCK_BOOTTIME, and it is similar to CLOCK_MONOTONIC. The second parameter here is the “&time”. We utilize the “printf” statement here, which is used for rendering the information on the output terminal. We want to print the time in seconds, so we utilize “time.tv_sec” here. It will return the time in seconds. Then we have “return 0,” and the code is completed here.

After opening the terminal, we add the first GCC command, which helps in the compilation of this c code. When this code is compiled, then we utilize the other command which is used for executing the C code. You can also see these commands here in the image below. It gives the result of the above code and returns the time in seconds here.

Example # 2

The “sys/time. h” and “time. h” header files are then added. To use the “clock_gettime ()” method in this code, we include this header file. The “main ()” function is then added, and an “int” data type is declared for it. Two parameters are passed to the “int main ()” method. For the “main ()” function, the first and second parameters are “int argc” and “char **argv,” respectively. Within the body of this main () function, “t” is declared as the “struct timespec.”

There are two parameters for this function. The “clock_gettime ()” function supports CLOCK BOOTTIME, which is like CLOCK_MONOTONIC. The “&t” is the second parameter. Here, the information is rendered on the output terminal using the “printf” statement. We use “t. tv_sec” because we want to display the time in seconds. The time in seconds will be returned. In the next “printf ()” function, we utilize the “t. tv_nsec,” so it will return time in nanoseconds.

Here is the result of this code, the time in seconds and also the time in nanoseconds displayed here. We get this output by typing some commands which are shown in this image.

Example # 3

We are going to utilize four different header files here because these header files are vital for this code. We are adding “stdio. h”, “stdlib. h”, “unistd. h”, and also “time. h” header files. Then we use “define” for declaring some constant value here. After this, we utilize the “int main ()” function and declare two different “struct timespec” with the name “start” and “stop”. Also, declare “accum” of the “double” data type. We put the “clock_gettime ()” function in the “if” statement and put a condition here. We are using “CLOCK_REALTIME” and “&start” here as the parameters of the “clock_gettime ()” function. If the given condition is true, then it will perform the statements which are here in the curly braces of this “if” statement.

The “perror” will render the error on the screen. We again utilize this “clock_gettime ()” function inside the “if” and again put the condition here. This time we pass “&stop” as the second parameter of this function. We again utilize the “perror” here for this “if” statement. Now, we initialize the “accum” here. The “tv_sec” is used for the time in seconds, and the “tv_nsec” is for the time in nanoseconds. The “printf” is utilized for displaying the result of the “accum” on the terminal. We also have the “return” statement at the last.

The output of example 3 is displayed here in the screenshot below. You can see the result of the “accum” here in this screenshot.

Conclusion

This guide demonstrates how the “clock_gettime ()” function in C programming operates. Here, we have provided a thorough explanation of the “clock_gettime ()” function as well as the practical use of the function. To make it simple for you to understand how to use the C “clock_gettime ()” function, we have also included the output here. We have explained that we use this “clock_gettime ()” function for retrieving the time currently displayed by the clock recognized by the clock id and writing it to the buffer. We anticipate that this guide will broaden your understanding of C programming functions.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.