Note 35 of advanced programming in UNIX Environment -- process time

We can measure three kinds of process time: wall clock time, user CPU time and system CPU time. Any process can call the times function to get itself

And the above values for terminating child processes.

  1. #include <sys/times.h>  
  2. clock_t times(struct tms* buf); //If successful, it will return the elapsed clock time on the wall (unit: clock ticks), if wrong, it will return - 1  
The structure of buf in the function is as follows:

  1. struct tms{  
  2. clock_t tms_utimes;  //user cpu time  
  3. clock_t tms_stimes;  //system cpu time  
  4. clock_t tms_cutime;  //user cpu time, terminated children  
  5. clock_t tms_cstime;  //system cpu time, terminated children  
  6. }  
To get the run time, you must get the relative value. For example, call times, save the return value, call times again at a later time, and subtract from the new return value

Previous return value, the difference is the wall clock time.

All the clock Φ T values returned by this function are transformed into seconds by SC Φ CLK Φ TCK (clock ticks per second returned by the sysconf function).

  1. #include <stdio.h>  
  2. #include <sys/times.h>  
  3. #include <time.h>  
  4. #include <unistd.h>  
  5.   
  6. static void pr_times(clock_t,struct tms*,struct tms*);  
  7. static void do_cmd(char *);  
  8.   
  9. int main(int argc, char *argv[]){  
  10.         int i;  
  11.         for(i=1; i<argc; i++){  
  12.                 do_cmd(argv[i]);  
  13.         }  
  14.         return 0;  
  15. }  
  16.   
  17. static void do_cmd(char* cmd){  
  18.         struct tms tmsstart, tmsend;  
  19.         clock_t start, end;  
  20.         int status;  
  21.         printf("command:%s\n",cmd);  
  22.         if((start=times(&tmsstart)) == -1){  
  23.                 perror("times");  
  24.                 return;  
  25.         }  
  26.   
  27.         if((status = system(cmd))<0){  
  28.                 perror("system");  
  29.                 return;  
  30.         }  
  31.   
  32.         if((end = times(&tmsend)) == -1){  
  33.                 perror("times");  
  34.                 return;  
  35.         }  
  36.   
  37.         pr_times(end-start, &tmsstart, &tmsend);  
  38. }  
  39.   
  40. static void pr_times(clock_t real, struct tms* tmsstart, struct tms* tmsend){  
  41.         static long clktck = 0;  
  42.         if((clktck = sysconf(_SC_CLK_TCK))<0){  
  43.                 perror("sysconf");  
  44.                 return;  
  45.         }  
  46.         printf("real:%7.2f\n",real/(double)clktck);  
  47.         printf("user:%7.2f\n",(tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);  
  48.         printf("sys:%7.2f\n",(tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);  
  49.         printf("child user:%7.2f\n",(tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);  
  50.         printf("child sys:%7.2f\n",(tmsend->tms_cstime - tmsstart->tms_cstime)/(double)clktck);  
  51. }  
Operation result:

yan@yan-vm:~/apue$ ./a.out "dd if=/dev/zero of=/dev/null bs=1M count=1000"
command:dd if=/dev/zero of=/dev/null bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 0.212456 s, 4.9 GB/s
real:   0.22
user:   0.00
sys:   0.00
child user:   0.01
child sys:   0.20

Posted by Rob the R on Sun, 05 Apr 2020 10:18:39 -0700