进程时间

进程时间

三种我们可以度量的时间:

  1. wall clock time
  2. user cpu time
  3. system cpu time

任何进程都能调用times函数去获得它自己或任何已经终止的子进程的三种时间。

#include <sys/times.h>

clock_t times(struct tms *buf);

Returns: elapsed wall clock time in clock ticks if OK, 1 on error

这个函数将填充buf指向的tms结构:

struct tms {
     clock_t  tms_utime;  /* user CPU time */
     clock_t  tms_stime;  /* system CPU time */
     clock_t  tms_cutime; /* user CPU time, terminated children */
     clock_t  tms_cstime; /* system CPU time, terminated children */
   };

注意:该结构中并不包含任何wall clock time。而是用wall clock time做为函数的返回值填充该结构。这个值是从过去某个时间点开始测量的,所以不能使用它的绝对值,而是使用它的相对值。例如:我们调用times并保存了它的返回值。在稍后,再次使用times,用返回的值减之前的那个值。得到的差就是wall clock time。(虽然可行,但是对于一个长时间运行的进程,保存的时间很有可能溢出)

其中两个字段的值是专门用于waited(wait, waitidwaitpid)的子进程。

由该函数返回的clock_t类型的值都会由每秒的滴答数转换成秒(每秒滴答数使用sysconf(_SC_CLK_TCK)得到)

Comments are closed.