获得进程执行时间

Linux中有很多获得时间信息的方法,但各自有各自的优缺点

1.使用time命令

# time ./a.out 
,,,//这是a.out 执行的内容

real    0m0.001s
user    0m0.001s
sys    0m0.001s

优点:可以直接获得进程的各种时间

缺点:只能在命令行执行

2.使用wait3获得进程的时间

相关结构体:

struct rusage {
        struct timeval ru_utime;
        struct timeval ru_stime;
        long   ru_maxrss;       
        long   ru_ixrss;        
        long   ru_idrss;        
        long   ru_isrss;        
        long   ru_minflt;       
        long   ru_majflt;       
        long   ru_nswap;        
        long   ru_inblock;      
        long   ru_oublock;      
        long   ru_msgsnd;       
        long   ru_msgrcv;       
        long   ru_nsignals;     
        long   ru_nvcsw;        
        long   ru_nivcsw;       
};

    struct timeval
  {
    __time_t tv_sec;        /* Seconds.  */
    __suseconds_t tv_usec;    /* Microseconds.  */
  };

 

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>

int main(void)
{
	pid_t pid;
	struct rusage my_rusage;
	if ((pid = fork()) < 0) 
	{
		perror("fork error");
		exit(-1);
	} else if (pid == 0) 
	{ 		
		printf("子进程PID = %ld, \n", (long)getpid());
		exit(0);
	} else 
	{
		int *stat_loc = malloc(sizeof(int));
		wait3(stat_loc, 0, &my_rusage);
		printf("子进程运行时间为:\n");
		printf("秒   = %ld \n", (long)my_rusage.ru_utime.tv_sec);   //秒
		printf("微秒 = %ld \n", (long)my_rusage.ru_utime.tv_usec);  //微秒
		
		printf("父进程PID = %ld, \n", (long)getpid());
		free(stat_loc);
		exit(0);		
	}
}

运行结果:

# ./a.out 
子进程PID = 11270, 
子进程运行时间为:
秒   = 0 
微秒 = 79 
父进程PID = 11269,

优点:可以获得很多进程的运行信息,包括时间信息

缺点:只能获得子进程的信息,还要等待子进程结束后,才能获得

3.使用times函数获得进程时间

相关结构体:

struct tms {
clock_t tms_utime; /* user CPU time */
clock_t tms_stime; /* system CPU time */
clock_t tms_cutime; /* user CPU time, terminated children */
clock_t tms_cstime; /* system CPU time, terminated children */
};

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>
#define bufsize 20
static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(void);

int
main(int argc, char *argv[])
{
	int i;
	setbuf(stdout, NULL);
	do_cmd(); /* once for each command-line arg */
	exit(0);
}
static void
do_cmd(void) /* execute and time the "cmd" */
{
	struct tms tmsstart, tmsend;
	clock_t start, end;

	if ((start = times(&tmsstart)) == -1) /* starting values */
		perror("times error");
	sleep(2);  //delay two second
	if ((end = times(&tmsend)) == -1) /* ending values */
		perror("times error");
	pr_times(end-start, &tmsstart, &tmsend);
}
static void
pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
{
	static long clktck = 0;
	if (clktck == 0) /* fetch clock ticks per second first time */
	{
		if ((clktck = sysconf(_SC_CLK_TCK)) < 0)
			perror("sysconf error");
	}
		
	printf(" real: %7.2f\n", real / (double) clktck);
	printf(" user: %7.2f\n",(tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
	printf(" sys: %7.2f\n",(tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck);
	printf(" child user: %7.2f\n",(tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);
	printf(" child sys: %7.2f\n",(tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);
}

优点:可以获得进程中任意时间段的时间信息,

缺点:

1.不能获取子进程的时间的时间信息,

2.它只能获取一段的信息,而不能获取整个进程的信息,

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值