【发布时间】: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 到底是什么意思。?
【问题讨论】:
我试图理解各种 sysconf 宏。我编写了一个程序如下。
int main()
{
fprintf(stdout, "No. of clock ticks per sec : %ld\n",sysconf(_SC_CLK_TCK));
return 0;
}
我总是得到 100 的结果。我在主频为 2.93GHz 的 CPU 上运行它。数字 100 到底是什么意思。?
【问题讨论】:
这只是每秒的时钟滴答数,在您的情况下,内核配置为每秒 100 个时钟(或 100Hz 时钟)。
【讨论】:
/proc/PID/stat 返回时钟滴答数的值时,这怎么可能? (字段 14-17,42-44。)如果它是动态的,那么这些值将完全没有用,因为您不知道在进程历史中的每个点上滴答的持续时间是多少。
每秒的时钟滴答数可以通过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 做任何事情。