#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í)行并且對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); //輸出對應(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("請輸入正確的命令.\n");
exit(0);
}
else
{
execute_cmd(argv[1]); //執(zhí)行命令
exit(0);
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:[Linux進(jìn)程]統(tǒng)計(jì)進(jìn)程的執(zhí)行時(shí)間-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article26/cohgcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、用戶體驗(yàn)、軟件開發(fā)、云服務(wù)器、搜索引擎優(yōu)化、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容