简体   繁体   English

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

[英]Measuring time of parent and children processes

I've written a program which forks in a loop. 我写了一个循环分叉的程序。 The only thing children processes do is to increase a counter and exit, whereas a parent process waits for each of them. 子进程执行的唯一操作是增加计数器并退出,而父进程等待它们中的每一个。

My goal is to measure user and system time of parent process and all his children separately. 我的目标是分别测量父进程和所有子进程的用户和系统时间。 I've succeded with parent process using times() function and struct tms. 我使用times()函数和struct tms成功完成了父进程。 Surprisingly, the same aproach to children processes isn't working. 令人惊讶的是,对儿童过程的同样方法是行不通的。 What is the mistake that I'm doing? 我在做什么错? How to measure those times? 如何衡量那些时间?

I've also tried getrusage() and I/it failed. 我也试过getrusage(),我/它失败了。

My code: 我的代码:

#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;
}

I think the problem is that your children are executing too quickly ; 我认为问题是你的孩子执行得太快 ; they don't take enough time to execute, so the sum of their time is plenty of zeros. 他们没有足够的时间来执行,所以他们的时间总和就是零。 To test this theory, I slightly changed your program: 为了测试这个理论,我略微改变了你的程序:

#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;
}

You'll see that I drastically cut down on the number of children, and made the children do some real work; 你会看到我彻底减少了孩子的数量,让孩子们做了一些真正的工作; 10_000 printf(... getpid()) operations. 10_000 printf(... getpid())操作。 Now the times amount to something: 现在时间相当于:

$ 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

I'm afraid your children just didn't have enough work to do to amount to anything. 我担心你的孩子没有足够的工作去做任何事情。 (Odd, sounds like parenting advice.) (奇怪,听起来像育儿的建议。)

Each child is given their own address space. 每个孩子都有自己的地址空间。 The code will not work because it will increment it's own local copy of counter and quit, leaving the version in the parent process/all other children untouched. 代码将无法工作,因为它将增加它自己的counter本地副本并退出,使父进程/所有其他子进程中的版本保持不变。

Also, you are very likely to get some errors with that many children. 此外,你很可能会遇到很多孩子的错误。

Sorry I could only help with half the program :(. 对不起,我只能帮助一半的程序:(。

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

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