简体   繁体   中英

Measuring time of process and its children

I am doing little assigment which involves fork,vfork and clone function. I need to measure user,system,real time of both the parent and all of the child processes. Measuring user and system time is simple, to measure real time, I call time from sys/times.h, store the value and child process calls

_exit(times(NULL)-procReal)

and I add this value to other variable (see code below).

My question is, should the value I store be computed just before fork or just after fork?

procReal=times(NULL);//here
#ifdef FORK
        pid=fork();
#elif VFORK
        pid=vfork();
#endif
 procReal=times(NULL);//or maybe here 
    if ( pid <0)
        error_sys_f("fork failed");
    else if (pid ==0)
    {
        foo();  
    }
    else
    {
        wait(&statLoc);
        if (WIFEXITED(statLoc))
            childrenReal+=WEXITSTATUS(statLoc);
        else
            error_sys_f("unnormal exit from children");
    }

procReal is a global variable.

when you fork a child there is another copy of procReal in the child's address space, not the same as the one in the parent. The value should be computed in the parent "before" fork and in the parent "after" wait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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