简体   繁体   English

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

[英]Interaction of fork() and clock_gettime() on Linux 2.6.x

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. 我需要监视一段包含派生的代码,并且我想确认在这种情况下,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. 我可以找到使用该技术的示例代码,但我担心时钟会在派生期间重置,因此减去结束时间和开始时间将毫无意义。

This description of fork() mentions reseting some timers, but does not mention how those timers are related to clock_gettime(). 在fork()的描述中提到了重置某些计时器,但是没有提及这些计时器与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 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_MONOTONIC_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. 编辑2:我可以在fork手册页中看到与time(2)关联的cpu计数器被重置,但是我看不到clock_gettime()的计数器被重置。 I don't see where it is documented that clock_gettime() uses the times(2) counters. 我看不到在哪里记录clock_gettime()使用times(2)计数器。 If it did, I would think that times(2) would be referenced on the clock_gettime man page. 如果可以的话,我认为可以在clock_gettime手册页上引用times(2)。

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. 正确的行为是为endtime在孩子做个小,贴近零值,如果您使用CLOCK_PROCESS_CPUTIME_ID ,因为fork它设置为零的孩子。 For the parent, it will be a value slightly larger than, but almost identical to starttime . 对于父级,它将是一个稍大于starttime的值,但几乎相同。
This is well-documented in the fork(2) manpage, and it is what makes sense too -- you create a new process. fork(2)联机帮助页中对此进行了很好的记录,这也很有意义-您创建了一个新流程。 The new process has, of course, not consumed any CPU time. 当然,新进程没有消耗任何CPU时间。 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). 另一方面,父级已经消耗了显着的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). 其他时钟(例如CLOCK_REALTIMECLOCK_MONOTONIC将不会受到影响(前者可以独立于您的分叉而由有特权的用户修改,但后者则不能)。
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. 它们是全局计时器,如果派生一个新进程(几乎所有时间都在发生)干扰了它们,那将是一场灾难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM