【问题标题】:sysconf(_SC_CLK_TCK) what does it return?sysconf(_SC_CLK_TCK) 它返回什么?
【发布时间】:2013-11-24 01:16:41
【问题描述】:

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

int main()
{
    fprintf(stdout, "No. of clock ticks per sec : %ld\n",sysconf(_SC_CLK_TCK));
    return 0;
}

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

【问题讨论】:

    标签: c linux kernel


    【解决方案1】:

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

    【讨论】:

    • 你似乎暗示这与CPU时钟没有任何关系?
    • @liv2hak 不相关,是定时器中断CPU进行调度等任务的次数,100Hz是常用值,频率越高定时器分辨率越高,开销越大。
    • 嗯,不完全是……现代 Linux 内核根据当前负载使用动态时钟频率(即,不再有周期性滴答声)。
    • @Claudio 当/proc/PID/stat 返回时钟滴答数的值时,这怎么可能? (字段 14-17,42-44。)如果它是动态的,那么这些值将完全没有用,因为您不知道在进程历史中的每个点上滴答的持续时间是多少。
    • 将其称为“调度程序时钟速率”是否正确?如果不是,什么是更合适的名字?我正在尝试在我的代码中命名一个变量,并且我已经有 5 个其他“时钟”可以跟踪完全不同的事物 lol
    【解决方案2】:

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

    printf ("_SC_CLK_TCK = %ld\n", sysconf (_SC_CLK_TCK));
    

    每秒时钟滴答的典型值为 100。也就是说,在这种情况下,每 10 毫秒或 0.01 秒有一个时钟滴答。要将由时间返回的clock_t 值转换为秒,必须除以每秒的时钟滴答数。使用 times 和 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); 
    }
    

    来源

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

    【讨论】:

    • 示例代码没有使用 _SC_CLK_TCK 做任何事情。
    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 2012-10-27
    • 2021-02-03
    • 2012-11-14
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多