sysconf(_SC_CLK_TCK)它返回什么?

我想了解各种sysconfmacros。我已经写了一个程序如下。

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时钟)。

每秒的时钟滴答数可以通过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