【发布时间】:2012-04-05 22:29:38
【问题描述】:
我正在做涉及 fork、vfork 和克隆功能的小任务。我需要测量父进程和所有子进程的用户、系统、实时。测量用户和系统时间很简单,要测量实时,我从 sys/times.h 中调用时间,存储值和子进程调用
_exit(times(NULL)-procReal)
然后我将此值添加到其他变量(参见下面的代码)。
我的问题是,我存储的值应该在 fork 之前还是 fork 之后计算?
procReal=times(NULL);//here
#ifdef FORK
pid=fork();
#elif VFORK
pid=vfork();
#endif
procReal=times(NULL);//or maybe here
if ( pid <0)
error_sys_f("fork failed");
else if (pid ==0)
{
foo();
}
else
{
wait(&statLoc);
if (WIFEXITED(statLoc))
childrenReal+=WEXITSTATUS(statLoc);
else
error_sys_f("unnormal exit from children");
}
procReal 是一个全局变量。
【问题讨论】: