#include <sys/times.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> //時(shí)間統(tǒng)計(jì)函數(shù) static void get_times(clock_t real, struct tms *tmsstart, struct tms *tmsend) { static long clktck = 0; if (clktck == 0) //第一次獲得時(shí)間 if ((clktck = sysconf(_SC_CLK_TCK)) < 0) { perror("調(diào)用sysconf函數(shù)錯(cuò)誤.\n"); } //以下為時(shí)間輸出 printf("時(shí)鐘時(shí)間: %7.2f\n", real / (double) clktck); printf("用戶CPU時(shí)間: %7.2f\n",(tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck); printf("系統(tǒng)CPU時(shí)間: %7.2f\n",(tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck); printf("子進(jìn)程時(shí)鐘時(shí)間: %7.2f\n",(tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck); printf("子進(jìn)程系統(tǒng)CPU時(shí)間: %7.2f\n",(tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck); } //執(zhí)行并且對(duì)cmd命令計(jì)時(shí) static void execute_cmd(char *cmd) { struct tms tmsstart, tmsend; //時(shí)間結(jié)構(gòu)體; clock_t start, end; //分別存放起始和停止時(shí)刻的時(shí)鐘滴答數(shù) int status; //執(zhí)行狀態(tài) printf("\n當(dāng)前執(zhí)行的命令是: %s\n", cmd); //輸出對(duì)應(yīng)的命令 if ((start = times(&tmsstart)) == -1) //獲得start時(shí)間 { perror("調(diào)用times函數(shù)出錯(cuò).\n"); } if ((status = system(cmd)) < 0) //執(zhí)行命令 { perror("調(diào)用system函數(shù)出錯(cuò)\n"); } if ((end = times(&tmsend)) == -1) //獲得end時(shí)間 { perror("調(diào)用times函數(shù)出錯(cuò)\n"); } get_times(end-start, &tmsstart, &tmsend); //計(jì)算運(yùn)行時(shí)間 } //主函數(shù) int main(int argc, char *argv[]) { int i; setbuf(stdout, NULL); //清空標(biāo)準(zhǔn)輸出(屏幕) if(argc != 2) //參數(shù)數(shù)目錯(cuò)誤 { printf("請(qǐng)輸入正確的命令.\n"); exit(0); } else { execute_cmd(argv[1]); //執(zhí)行命令 exit(0); } }
當(dāng)前標(biāo)題:[Linux進(jìn)程]統(tǒng)計(jì)進(jìn)程的執(zhí)行時(shí)間
文章分享:http://muchs.cn/article6/gdohog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、做網(wǎng)站、定制網(wǎng)站、搜索引擎優(yōu)化、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)