在linux上测量exec()-ed进程所用的时间
我使用times()函数来测量值,但我不确定我的方法是否正确。请看一看并提出建议在linux上测量exec()-ed进程所用的时间,c,linux,ipc,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);
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()
之前,您需要调用次(&tms\u start)
。在上面的代码中,tms\u start
变量在父级中未初始化,因为父级从未调用次(&tms\u start)
struct tms tms_start,tms_end;
时间(&tms_开始);//啊,我明白了。我想这个差值计算是正确的?clock_t real=tms_end.tms_cstime-tms_start.tms_stime如果您想测量内核代表子进程所花费的时间,那么您的表达式是正确的。但是,如果(更有可能)您想要测量进程本身占用的CPU时间,则使用tms_cutime和tms_utime。或者,如果您想测量实际使用的挂钟时间,请不要使用times(),而是使用time()或gettimeofday()。stop去哪里了?
struct tms tms_start, tms_end;
times(&tms_start); // <-- here
if (!(pid=fork()))
{
//some necessary operations here
execl(...);
}
else if (pid)
{
...