测量linux上的exec() - ed进程所花费的时间

时间:2022-09-06 16:31:52

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

我正在使用times()函数来测量值,但我不确定我的方法是否正确。请看看和建议

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

1 个解决方案

#1


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

在调用fork()之前,您需要调用times(&tms_start)。在上面的代码中,tms_start变量在父级中未初始化,因为父级从不调用次数(&tms_start)。

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

#1


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

在调用fork()之前,您需要调用times(&tms_start)。在上面的代码中,tms_start变量在父级中未初始化,因为父级从不调用次数(&tms_start)。

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