I was trying to understand various sysconf macros.I have written a program as below.
int main()
{
fprintf(stdout, "No. of clock ticks per sec : %ld\n",sysconf(_SC_CLK_TCK));
return 0;
}
I always get the result as 100.I am running it on a CPU that is clocked at 2.93GHz.What does the number 100 exactly mean.?
It's just the number of clock ticks per second, in your case the kernel is configured for 100 clocks per second (or 100Hz clock).
The number of clock ticks per second can be found by the sysconf
system call,
printf ("_SC_CLK_TCK = %ld\n", sysconf (_SC_CLK_TCK));
A typical value of clock ticks per second is 100. That is, in this case, there is a clock tick every 10 milliseconds or 0.01 second. To convert the clock_t
values, returned by times, into seconds one has to divide by the number of clock ticks per second. An example program using the times and sysconf (_SC_CLK_TCK)
system calls is,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/times.h>
int main ()
{
clock_t ct0, ct1;
struct tms tms0, tms1;
int i;
if ((ct0 = times (&tms0)) == -1)
perror ("times");
printf ("_SC_CLK_TCK = %ld\n", sysconf (_SC_CLK_TCK));
for (i = 0; i < 10000000; i++)
;
if ((ct1 = times (&tms1)) == -1)
perror ("times");
printf ("ct0 = %ld, times: %ld %ld %ld %ld\n", ct0, tms0.tms_utime,
tms0.tms_cutime, tms0.tms_stime, tms0.tms_cstime);
printf ("ct1 = %ld, times: %ld %ld %ld %ld\n", ct1, tms1.tms_utime,
tms1.tms_cutime, tms1.tms_stime, tms1.tms_cstime);
printf ("ct1 - ct0 = %ld\n", ct1 - ct0);
}
Source:
http://www.softprayog.in/tutorials/linux-process-execution-time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With