times返回的buf中的结构体成员的值为什么每次都是0

wukewuke666 wukewuke666 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <glob.h>
#include <sys/times.h>
#include <time.h>
extern char **environ;
int main(int argc, char *argv[])
{
    glob_t globres;
    int i = 0;
    struct tms startbuf;
    struct tms endbuf;
    long int start,end;
    if ((start=times(&startbuf) )== -1)
    {
        perror("times()");
        exit(1);
    }
    long clockticks = sysconf(_SC_CLK_TCK);
    if (argc < 2)
    {
        fprintf(stderr, "Usage:%s <file> <argument>\n", argv[0]);
        exit(1);
    }
    pid_t pid;
    pid = fork();
    if (pid < 0)
    {

        perror("fork()");
        exit(1);
    }
    if (pid == 0) // 子进程
    {

        for (i = 1; i < argc; i++)
        {
            if (i == 1)
                glob(argv[i], GLOB_NOCHECK, NULL, &globres);
            else
                glob(argv[i], GLOB_NOCHECK | GLOB_APPEND, NULL, &globres);
        }
        execvp(argv[1], globres.gl_pathv);
        perror("execvp()");
        exit(1);
    }
    else if (pid > 0) // 父进程
    {
        wait(NULL);
        if ((end=times(&endbuf)) == -1)
        {
            perror("times()");
            exit(1);
        }
        printf("start=%ld end=%ld clock_per=%ld\n",start,end,clockticks);
        //访问里面的时间还是0?
        printf("%ld\n%ld\n%ld\n%ld\n", startbuf.tms_cstime, startbuf.tms_cutime, startbuf.tms_stime, startbuf.tms_utime);
        printf("%ld\n%ld\n%ld\n%ld\n", endbuf.tms_cstime, endbuf.tms_cutime, endbuf.tms_stime, endbuf.tms_utime);
        //printf("real   %fs\nuser   %fs\nsys   %fs\n",(double)((buf.tms_cutime+buf.tms_cstime)/clockticks) \
                 ,(float)(buf.tms_cutime/clockticks),(float)(buf.tms_cstime/clockticks));
    }
    exit(0);
}

为什么我的endbuf.tms_cstime和startbuf.tms_cstime全是0,endbuf和startbuf里面的成员的值为什么都为0

 

...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部