0
点赞
收藏
分享

微信扫一扫

获得进程执行时间

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.它只能获取一段的信息,而不能获取整个进程的信息,

 

举报

相关推荐

0 条评论