25

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.?

liv2hak
  • 14,472
  • 53
  • 157
  • 270

2 Answers2

22

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).

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • 2
    you seem to suggest that this is in no way related to the CPU clock? – liv2hak Nov 12 '13 at 02:49
  • 9
    @liv2hak no it's not related, it's the number of times the timer interrupts the CPU for scheduling and other tasks, 100Hz is a common value, higher frequency equals higher timer resolution and more overhead. – iabdalkader Nov 12 '13 at 02:54
  • 4
    Well, not exactly... Modern Linux kernels use a dynamic clock rate depending on the current load (i.e., no more periodic ticks). – Claudio Feb 25 '16 at 11:22
  • 2
    @Claudio How can that be when `/proc/PID/stat` returns values in number of clock ticks? (Fields 14-17,42-44.) If it were dynamic, these values would be completely useless because you don't know what the duration of a tick was at each point in the process' history. – Luc Apr 11 '21 at 13:43
  • Would it be correct to call this the "scheduler clock rate"? If not, what would be a more appropriate name? I'm trying to name a variable in my code, and I already have like 5 other "clock"s that track totally different things lol – Alexander Aug 20 '21 at 00:50
1

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

coterobarros
  • 941
  • 1
  • 16
  • 25
user3163041
  • 145
  • 1
  • 4