【问题标题】:Measuring time of process and its children测量过程及其子进程的时间
【发布时间】: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 是一个全局变量。

【问题讨论】:

    标签: c fork


    【解决方案1】:

    当你 fork 一个孩子时,在孩子的地址空间中有另一个 procReal 副本,与父级中的不一样。该值应在父“之前”分叉和父“之后”等待中计算。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-28
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多