fork()和clock_gettime()在Linux 2.6.x上的交互

时间:2021-07-06 05:35:36

I need to monitor a section of code that includes a fork and I want to confirm that clock_gettime() is documented as working for this scenario.

我需要监视包含fork的一段代码,并且我想确认clock_gettime()被文档化以用于此场景。

Here is the pseudo code:

下面是伪代码:

starttime = clock_gettime()

if (fork() == 0) {
   /* in the child */
   endtime = clock_gettime()
   print "elapsed time is " endtime - starttime
}

I can find example code where this technique is used, but I am concerned that the clock will get reset during the fork, and then the subtraction of endtime and starttime will be meaningless.

我可以找到使用这种技术的示例代码,但是我担心在fork中时钟将被重置,然后对endtime和starttime的减法将毫无意义。

This description of fork() mentions reseting some timers, but does not mention how those timers are related to clock_gettime(). By omission, one could assume that it is not affected, but I would like a bit more assurance before I rely on this technique. http://man7.org/linux/man-pages/man2/fork.2.html

对fork()的描述提到了对一些计时器的重新设置,但是没有提到这些计时器与clock_gettime()的关系。通过省略,我们可以假定它不受影响,但是在我依赖此技术之前,我希望有更多的保证。http://man7.org/linux/man-pages/man2/fork.2.html

I can write a test program to find out what happens, but I am more interested in finding the documentation on what is defined as the correct behavior.

我可以编写一个测试程序来找出发生了什么,但是我更感兴趣的是找到关于定义为正确行为的文档。

EDIT: I plan in using CLOCK_PROCESS_CPUTIME_ID and CLOCK_MONOTONIC_RAW

编辑:我计划使用CLOCK_PROCESS_CPUTIME_ID和clock_单调的ic_raw

EDIT 2: I can see in the fork man page that the cpu counters associated with times(2) are reset, but I don't see where the counters for clock_gettime() are reset. I don't see where it is documented that clock_gettime() uses the times(2) counters. If it did, I would think that times(2) would be referenced on the clock_gettime man page.

编辑2:我可以在fork man页面中看到与times(2)相关的cpu计数器被重置,但是我看不到clock_gettime()的计数器被重置的位置。我不知道clock_gettime()使用times(2)计数器的文档说明在哪里。如果是这样,我就会认为时间(2)将在clock_gettime手册页中引用。

1 个解决方案

#1


3  

The correct behavior is for endtime in the child to be a small, close-to-zero value if you use CLOCK_PROCESS_CPUTIME_ID, since fork sets it to zero for the child. For the parent, it will be a value slightly larger than, but almost identical to starttime.
This is well-documented in the fork(2) manpage, and it is what makes sense too -- you create a new process. The new process has, of course, not consumed any CPU time. It's a newborn process, after all! The parent, on the other hand, has already consumed noticeable CPU and continues doing so, there is no good reason why its counter should be reset to zero (that would be a lie).

如果使用CLOCK_PROCESS_CPUTIME_ID,则正确的行为是将子节点中的endtime设置为一个很小的、接近于零的值,因为fork将子节点的值设置为零。对于父类,它的值将略大于,但几乎与starttime相同。这在fork(2)手册中有很好的文档说明,这也是有意义的——您创建一个新流程。当然,新进程没有消耗任何CPU时间。毕竟,这是一个新生的过程!另一方面,父进程已经消耗了明显的CPU,并且还在继续这样做,因此没有理由将它的计数器重置为零(这是一个谎言)。

Other clocks like CLOCK_REALTIME or CLOCK_MONOTONIC will not be affected (the former can independently of you forking be modified by a privilegued user, but the latter cannot).
That, too, is according to what common sense tells you. They're global timers, if forking a new process (which happens nearly all the time) was interfering with them, it would be a desaster.

其他时钟如CLOCK_REALTIME或clock_单调时钟不会受到影响(前者可以独立于您的分配器,由特权用户修改,但后者不能)。这也是根据常识告诉你的。它们是全局计时器,如果分出一个新进程(几乎一直都在发生)会干扰它们,那么它就是一个暴君。

#1


3  

The correct behavior is for endtime in the child to be a small, close-to-zero value if you use CLOCK_PROCESS_CPUTIME_ID, since fork sets it to zero for the child. For the parent, it will be a value slightly larger than, but almost identical to starttime.
This is well-documented in the fork(2) manpage, and it is what makes sense too -- you create a new process. The new process has, of course, not consumed any CPU time. It's a newborn process, after all! The parent, on the other hand, has already consumed noticeable CPU and continues doing so, there is no good reason why its counter should be reset to zero (that would be a lie).

如果使用CLOCK_PROCESS_CPUTIME_ID,则正确的行为是将子节点中的endtime设置为一个很小的、接近于零的值,因为fork将子节点的值设置为零。对于父类,它的值将略大于,但几乎与starttime相同。这在fork(2)手册中有很好的文档说明,这也是有意义的——您创建一个新流程。当然,新进程没有消耗任何CPU时间。毕竟,这是一个新生的过程!另一方面,父进程已经消耗了明显的CPU,并且还在继续这样做,因此没有理由将它的计数器重置为零(这是一个谎言)。

Other clocks like CLOCK_REALTIME or CLOCK_MONOTONIC will not be affected (the former can independently of you forking be modified by a privilegued user, but the latter cannot).
That, too, is according to what common sense tells you. They're global timers, if forking a new process (which happens nearly all the time) was interfering with them, it would be a desaster.

其他时钟如CLOCK_REALTIME或clock_单调时钟不会受到影响(前者可以独立于您的分配器,由特权用户修改,但后者不能)。这也是根据常识告诉你的。它们是全局计时器,如果分出一个新进程(几乎一直都在发生)会干扰它们,那么它就是一个暴君。