繁体   English   中英

测量父母和子女过程的时间

[英]Measuring time of parent and children processes

我写了一个循环分叉的程序。 子进程执行的唯一操作是增加计数器并退出,而父进程等待它们中的每一个。

我的目标是分别测量父进程和所有子进程的用户和系统时间。 我使用times()函数和struct tms成功完成了父进程。 令人惊讶的是,对儿童过程的同样方法是行不通的。 我在做什么错? 如何衡量那些时间?

我也试过getrusage(),我/它失败了。

我的代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#include <time.h>

#ifndef COUNT
#define COUNT 100000
#endif



int counter;


int main(){

struct tms time1,time2;
times(&time1);

int count = COUNT;
pid_t pid;
while(count--){
    if((pid=fork())<0){
        printf("fork error\n");
    } else if(pid==0){ /* child */
        counter++;
        _exit(0);
    } else {
        waitpid(pid,NULL,0); /*wait()*/
    }
}
printf("COUNTER: %d\n",counter);



times(&time2);

long double clktck=sysconf(_SC_CLK_TCK);
double user=(time2.tms_utime-time1.tms_utime)/(double)clktck;
double system=(time2.tms_stime-time1.tms_stime)/(double)clktck;
double cuser=(time2.tms_cutime-time1.tms_cutime)/(double)clktck;
double csystem=(time2.tms_cstime-time1.tms_cstime)/(double)clktck;

printf("USER:%lf\nSYSTEM:%lf\n",user,system);
printf("CUSER:%lf\nCSYSTEM:%lf\n",cuser,csystem);



return 0;
}

我认为问题是你的孩子执行得太快 ; 他们没有足够的时间来执行,所以他们的时间总和就是零。 为了测试这个理论,我略微改变了你的程序:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#include <time.h>

#ifndef COUNT
#define COUNT 100
#endif



int counter;


int main(){

struct tms time1,time2;
times(&time1);

int count = COUNT;
pid_t pid;
while(count--){
    if((pid=fork())<0){
        printf("fork error\n");
    } else if(pid==0){ /* child */
        int i;
        for (i=0; i<10000; i++) {
            printf("in child %i\n", getpid());
        }
        exit(0);
    } else {
        waitpid(pid,NULL,0); /*wait()*/
    }
}
printf("COUNTER: %d\n",counter);



times(&time2);

printf("%lu %lu %lu %lu\n", time2.tms_utime, time2.tms_stime, time2.tms_cutime, time2.tms_cstime);

long double clktck=sysconf(_SC_CLK_TCK);
double user=(time2.tms_utime-time1.tms_utime)/(double)clktck;
double system=(time2.tms_stime-time1.tms_stime)/(double)clktck;
double cuser=(time2.tms_cutime-time1.tms_cutime)/(double)clktck;
double csystem=(time2.tms_cstime-time1.tms_cstime)/(double)clktck;

printf("USER:%lf\nSYSTEM:%lf\n",user,system);
printf("CUSER:%lf\nCSYSTEM:%lf\n",cuser,csystem);



return 0;
}

你会看到我彻底减少了孩子的数量,让孩子们做了一些真正的工作; 10_000 printf(... getpid())操作。 现在时间相当于:

$ time ./times
...
in child 16181
COUNTER: 0
1 0 24 95
USER:0.010000
SYSTEM:0.000000
CUSER:0.240000
CSYSTEM:0.950000

real    0m2.234s
user    0m0.250s
sys 0m0.950s

我担心你的孩子没有足够的工作去做任何事情。 (奇怪,听起来像育儿的建议。)

每个孩子都有自己的地址空间。 代码将无法工作,因为它将增加它自己的counter本地副本并退出,使父进程/所有其他子进程中的版本保持不变。

此外,你很可能会遇到很多孩子的错误。

对不起,我只能帮助一半的程序:(。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM