时间以及执行所有命令行参数

来源:互联网 发布:蜜桃影院源码 编辑:程序博客网 时间:2024/05/29 14:22
#include <stdio.h>#include <stdlib.h>#include <sys/times.h>#include <unistd.h>static void do_cmd( char * );static void pr_times( clock_t, struct tms *, struct tms  *);int main( int argc, char *argv[] ){inti;setbuf( stdout, NULL );for( i = 1; i < argc; i++ )do_cmd( argv[i] );exit( 0 );}static void do_cmd( char *cmd ){struct tmstmsstart, tmsend;clock_tstart, end;intstatus;printf( "\ncommand: %s\n", cmd );if( (start = times( &tmsstart )) < 0 )printf( "times error\n" );if( (status = system( cmd )) < 0 )printf( "system error\n" ); if(( end = times( &tmsend ) ) < 0 ) printf( "times error\n" );pr_times( end - start, &tmsstart, &tmsend );}static void pr_times( clock_t real, struct tms *tmsstart, struct tms *tmsend ){staticlongclktck = 0;if( clktck == 0 )if(( clktck = sysconf(_SC_CLK_TCK ) ) < 0 )printf( "sysconf error\n" );printf( "real:%7.2f\n", real / ( double )clktck );printf( "user:%-7.2f\n",(double)(tmsend->tms_utime - tmsstart->tms_utime) / clktck );printf( "sys:%7.3f\n",(double)(tmsend->tms_stime - tmsstart->tms_stime ) / clktck );}


运行结果如下:

wangkai@ubuntu:~/Test$ ./a.out "sleep 5" "date"command: sleep 5real:   5.00user:0.00   sys:  0.000command: date2012年 05月 26日 星期六 06:07:43 PDTreal:   0.01user:0.00   sys:  0.000wangkai@ubuntu:~/Test$ 


 

原创粉丝点击