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