测量所用的时间由一个exec() - 在Linux上编过程 [英] Measuring time taken by an exec()-ed process on linux

查看:168
本文介绍了测量所用的时间由一个exec() - 在Linux上编过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的时间()函数来衡量的价值,但我不知道如果我的做法是正确的。请看看和建议

 结构TMS tms_start,tms_end;
如果(!(PID =叉()))
{
    //此处一些必要的操作
    次(安培; tms_start);
    EXECL(...);
}
否则如果(PID)
{
    //父
    INT状态;
    等待(安培;状态);
    次(安培; tms_end);
    如果(WIFEXITED(状态))
    {
        如果(WEXITSTATUS(状态)== 0)
        {
            clock_t表示真正= tms_end.tms_cstime - tms_start.tms_stime
            浮running_time =真正的/(双)的sysconf(_SC_CLK_TK);
        }
    }
}


解决方案

您需要调用倍(安培; tms_start)调用之前叉()。在你上面的code时, tms_start 变量是在父未初始化,因为父母从来没有要求倍(安培; tms_start)

 结构TMS tms_start,tms_end;
次(安培; tms_start); //< - 在这里
如果(!(PID =叉()))
{
    //此处一些必要的操作
    EXECL(...);
}
否则如果(PID)
{
    ...

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

这篇关于测量所用的时间由一个exec() - 在Linux上编过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆