sysconf(_SC_CLK_TCK)返回什么?

时间:2013-11-12 02:39:20

标签: c linux kernel

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

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

我总是得到100的结果。我在时钟频率为2.93GHz的CPU上运行。数字100的确切含义是什么。?

2 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:0)

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