Linux sysconf(_SC_CLK_TCK) 它返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19919881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:18:57  来源:igfitidea点击:

sysconf(_SC_CLK_TCK) what does it return?

clinuxkernel

提问by liv2hak

I was trying to understand various sysconf macros.I have written a program as below.

我试图理解各种 sysconf 宏。我编写了一个程序,如下所示。

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

我总是得到 100 的结果。我在主频为 2.93GHz 的 CPU 上运行它。数字 100 到底是什么意思。?

回答by iabdalkader

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

这只是每秒时钟滴答数,在您的情况下,内核配置为每秒 100 个时钟(或 100Hz 时钟)。

回答by user3163041

The number of clock ticks per second can be found by the sysconf system call,

每秒时钟滴答数可以通过 sysconf 系统调用找到,

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,

每秒时钟滴答的典型值为 100。也就是说,在这种情况下,每 10 毫秒或 0.01 秒就有一个时钟滴答。要将由时间返回的clock_t 值转换为秒,必须除以每秒时钟滴答数。使用时间和 sysconf (_SC_CLK_TCK) 系统调用的示例程序是,

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <time.h> 
#include <sys/times.h> 

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

http://www.softprayog.in/tutorials/linux-process-execution-time