UNIX环境高级编程笔记(9)- 进程时间times

 


 


前言

本章主要介绍进程时间times的使用,struct tms结构体,以及times使用实例。


 

一、times函数

#include<sys/times.h>

clock_t times(struct tms *buf);

二、struct tms结构体

struct tms{

clock_t tms_utime;// 用户CPU时间

clock_t tms_stime;//系统CPU时间

clock_t tms_cutime;//终止的子进程,用户CPU时间

clock_t tms_cstime;//终止的子进程,系统CPU时间

};

二、例程

1.times()函数

代码如下(示例):

static void pr_times(clock_t real,struct tms *tmsstart,struct tms *tmsend)
{
    static long clktck = 0;

    if(clktck == 0)
        if((clktck = sysconf(_SC_CLK_TCK)) < 0)
            err_sys("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);

}
static void do_cmd(char *cmd)
{
    struct tms tmsstart,tmsend;
    clock_t start,end;
    int status;

    printf("\ncommand: %s\n",cmd);

    if((start=times(&tmsstart)) == -1)
        err_sys("times error");
    if((status = system(cmd)) < 0)
        err_sys("system() error");
    if((end=times(&tmsend)) == -1)
        err_sys("times error");

    pr_times(end-start,&tmsstart,&tmsend);
    pr_exit(status);


}
void unix_8_31_times(int num,char **name)
{
    int i;
    setbuf(stdout,NULL);
    for(i=1;i<num;i++)
        do_cmd(name[i]);
    exit(0);
}
#define UNIX_8_31_TIMES
int main(int argc, char *argv[])
{
#ifdef UNIX_8_31_TIMES
    if(argc < 2)
        err_quit("usage: Is directory name");
    unix_8_31_times(argc,argv);
#endif
    printf("MAIN END\n");
    return 0;
}代码结果:
代码说明:所有由此函返回的clock_t值都用_SC_CLK_TCK(由sysconf函数返回的每秒始终滴答数)转化成秒

 


总结

本章主要介绍了进程时间times函数的使用。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默默的赶路人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值