【问题标题】:Measuring time taken by an exec()-ed process on linux测量 linux 上 exec()-ed 进程所花费的时间
【发布时间】:2009-07-02 19:56:17
【问题描述】:

我正在使用 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);
        }
    }
}

【问题讨论】:

    标签: c linux ipc


    【解决方案1】:

    您需要先致电times(&tms_start),然后再致电fork()。在上面的代码中,tms_start 变量在父级中未初始化,因为父级从不调用 times(&tms_start)

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

    【讨论】:

    • 啊,我明白了。所以我猜这个差异计算是正确的吗? clock_t real = tms_end.tms_cstime - tms_start.tms_stime
    • 如果你想测量内核代表子进程所花费的时间,那么你的表达是正确的。但是,如果(更有可能)您想测量进程本身占用的 CPU 时间,则使用 tms_cutime 和 tms_utime。或者,如果您想测量实际的挂钟时间,请不要使用 times(),而是使用 time() 或 gettimeofday()。
    • 停在哪里?
    猜你喜欢
    • 2011-06-15
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多