简体   繁体   中英

Measuring time taken by an exec()-ed process on linux

I'm using the times() function to measure the value but I'm not sure if my approach is correct. Please have a look and advice

struct tms tms_start, tms_end;
if (!(pid=fork()))
{
    //some necessary operations here
    times(&tms_start);
    execl(...);
}
else if (pid)
{
    //in parent
    int status;
    wait(&status);
    times(&tms_end);
    if (WIFEXITED(status))
    {
        if(WEXITSTATUS(status)==0)
        {
            clock_t real = tms_end.tms_cstime - tms_start.tms_stime
            float running_time = real/(double)sysconf(_SC_CLK_TK);
        }
    }
}

You'll need to call times(&tms_start) before the call to fork() . In your code above, the tms_start variable is uninitialised in the parent since the parent never calls times(&tms_start) .

struct tms tms_start, tms_end;
times(&tms_start);             // <-- here
if (!(pid=fork()))
{
    //some necessary operations here
    execl(...);
}
else if (pid)
{
    ...

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