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

标签 c linux ipc

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

最佳答案

在调用 fork() 之前,您需要调用 times(&tms_start)。在上面的代码中,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)
{
    ...

关于c - 测量 Linux 上 exec()-ed 进程所花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1076374/

相关文章:

c - 如何将数组传递给这样的函数 : void fooboo(char array[i]);

c - 如何访问堆上的数组链表?

在 C 中使用 pow 时,CMake 能否检测到我是否需要链接到 libm?

c - 将数组及其值的个数传递给 C 中的函数

c - 如何使用 C 获得类似于 Linux top 的输出

linux - 更改 emacs 中的默认 find-grep 命令

linux - 如何找出程序访问了哪些文件?

linux - 何时对客户端-服务器 IPC 使用基于 Fifo 的套接字?

python - 来自 Python 的 C 信号量

c# - 将消息从一个正在运行的控制台应用程序发送到另一个