10

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. 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.

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

2 Answers 2

6

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. 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.)

2
  • It seems to you are right, @sarnold. Thanks. But there's one thing that bothers me. Why total time aquired by times() function isn't equal to real time from time command? Apr 10, 2011 at 17:11
  • @Pawel, it's a matter of scheduling and stochastic measurement; I don't know what else my computer was doing when I was running these tasks, but my urxvt was scrolling like mad and my X11 was redrawing the terminal. The kernel will account one 'click' to whichever processes were running when the click happens -- and whichever process is running might not be the one that actually requested IO operations. There have been attempts to improve the accuracy, but the seem to be more expensive than simple heuristics. (And, since most processes last for several seconds, it mostly works.)
    – sarnold
    Apr 11, 2011 at 0:47
2

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.

Also, you are very likely to get some errors with that many children.

Sorry I could only help with half the program :(.

3
  • 1
    Since the parent waits for each child to die before fork()ing the next, it ought to be fine. There's only ever one child process active, not the full 100_000 children. :) And perhaps @Pawel didn't actually want the children to modify the parent's counter, so much as make sure that each one was forced to modify its address space in some way before dying. (Though the fact that he prints the value of the counter does support your interpretation.)
    – sarnold
    Apr 10, 2011 at 0:57
  • Having counter increased is not my goal. If it were I would use vfork() call instead of fork() :) What's important to me is time. While using times() function as above or getrusage() I get 0.0s which certainly means that it's not total time of children processes Apr 10, 2011 at 1:15
  • Since you only increase one variable, the time reported won't be much above 0.0s. Try doing some lengthly stuff instead and see what happens.
    – sl0815
    Apr 10, 2011 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.