《unix高级环境编程》进程控制——进程时间

来源:互联网 发布:外贸进出口软件哪家好 编辑:程序博客网 时间:2024/05/21 17:50

进程时间

        进程时间有墙上时钟时间、用户CPU时间和系统CPU时间。任一进程都可以调用 times 函数以获得它自己以及终止子进程的上述值。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /* 进程时间 */  
  2.   
  3. /* 
  4.  * 函数功能:获取进程的时间:墙上时钟时间、用户CPU时间和系统CPU时间; 
  5.  * 返回值:若成功则返回流逝的墙上时钟时间(单位:时钟滴答数),若出错则返回-1; 
  6.  * 函数原型: 
  7.  */  
  8. #include <sys/times.h>  
  9. clock_t times(struct tms *buf);  
  10.   
  11. /* 
  12.  * struct tms 结构如下: 
  13.  */  
  14. struct tms{  
  15.   
  16.     clock_t tms_utime;  /* user CPU time */  
  17.     clock_t tms_stime;  /* system CPU time */  
  18.     clock_t tms_cutime;  /* user CPU time, terminated children */  
  19.     clock_t tms_cstime;  /* system CPU time, terminated children */  
  20. };  
      在进程时间结构 tms 中并没有包含墙上时钟时间的测量值,times 函数返回值可作为墙上时钟时间的测量值,其取值是两次times 返回值的差值。由此函数返回的 clock_t 值都用_SC_CLK_TCK(由 sysconf函数返回的每秒时钟滴答数)变换成秒数。

测试程序:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <sys/times.h>  
  2. #include <time.h>  
  3. #include "apue.h"  
  4.   
  5. static void pr_times(clock_tstruct tms *, struct tms *);  
  6. static void do_cmd(char *);  
  7.   
  8. void pr_exit(int status);  
  9. int main(int argc, char *argv[])  
  10. {  
  11.         int i;  
  12.         for(i=1; i<argc; i++){  
  13.                 do_cmd(argv[i]);  
  14.         }  
  15.         exit(0);  
  16. }  
  17.   
  18. static void do_cmd(char* cmd)  
  19. {  
  20.         struct tms tmsstart, tmsend;  
  21.         clock_t start, end;  
  22.         int status;  
  23.         printf("\ncommand: %s\n", cmd);  
  24.         if((start=times(&tmsstart)) == -1)  
  25.                 err_sys("times error");  
  26.   
  27.         if((status = system(cmd)) < 0)  
  28.                 err_sys("system error");  
  29.   
  30.         if((end = times(&tmsend)) == -1)  
  31.                 err_sys("times error");  
  32.   
  33.         pr_times(end-start, &tmsstart, &tmsend);  
  34.         pr_exit(status);  
  35. }  
  36.   
  37. static void pr_times(clock_t real, struct tms* tmsstart, struct tms* tmsend)  
  38. {  
  39.         static long clktck = 0;  
  40.         if((clktck = sysconf(_SC_CLK_TCK)) < 0)  
  41.                 err_sys("sysconf error");  
  42.         printf("real: %7.2f\n",real/(double)clktck);  
  43.         printf("user: %7.2f\n",(tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);  
  44.         printf("sys: %7.2f\n",(tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);  
  45.         printf("child user: %7.2f\n",(tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);  
  46.         printf("child sys: %7.2f\n",(tmsend->tms_cstime - tmsstart->tms_cstime)/(double)clktck);  
  47. }  
  48. void pr_exit(int status)  
  49. {  
  50.     if(WIFEXITED(status))  
  51.         printf("normal termination, exit status = %d\n", WEXITSTATUS(status));  
  52.     else if(WIFSIGNALED(status))  
  53.         printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status),  
  54. #ifdef WCOREDUMP  
  55.                 WCOREDUMP(status) ? "(core file generated)" : " ");  
  56. #else  
  57.     " ");  
  58. #endif  
  59.   
  60.     else if(WIFSTOPPED(status))  
  61.         printf("child stoped, signal number = %d\n", WSTOPSIG(status));  
  62. }  
输出结果:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 执行:./time "date"  
  2. 输出结果:  
  3. command: date  
  4. Fri Nov  7 20:31:13 CST 2014  
  5. real:    0.00  
  6. user:    0.00  
  7. sys:    0.00  
  8. child user:    0.00  
  9. child sys:    0.00  
  10. normal termination, exit status = 0  
0 0
原创粉丝点击