为什么 tv_sec 和 tv_usec 对于确定计时器的持续时间都很重要?
手册页 gettimer(2) 声称
tv_sec 和 tv_usec 对于确定 计时器的持续时间
它没有继续说明为什么会这样。在我遇到的许多例子中,tv_sec 简单地设置为 0,而 tv_usec 则被赋予一些合理的值,反之亦然。这些计时器是同时倒计时,还是总倒计时时间为 tv_sec + tv_usec?我应该同时使用两者吗?两者都不?
The manual page getitimer(2) claims that
both tv_sec and tv_usec significant in determining the
duration of a timer
It doesn't go on to say why that is. In many examples that I have come across tv_sec is simply set to 0, while tv_usec is given some reasonable value, or vis versa. Are these timers counting down simultaneously, or is the total countdown time tv_sec + tv_usec? Should I use both? Neither?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
手册页记录了
timeval
结构:如果您想等待整秒数,只需设置
tv_sec
。如果您想等待一秒钟,您可以设置tv_usec
。如果您想等待 4.5 秒,则可以将它们两者设置为适当的值(分别为 4 和 500000)The man page documents the
timeval
structure:If you want to wait a whole number of seconds, you'd just set
tv_sec
. If you want to wait a portion of a second, you'd settv_usec
. If you want to wait 4.5 seconds, you'd set both of them to appropriate values (4 and 500000, respectively)是的,总时间是两者的总和。 tv_sec 是秒。而 tv_usec 则超出此数微秒。
Yes, the total time is the sum of both. tv_sec is the seconds. And tv_usec is microseconds beyond that.
但其结构是这样描述的:
如您所见,总时间为 tv_sec + (1.0/1000000) * tv_usec 秒。这就是为什么当您需要的时间低于 1 秒时,您需要设置
tv_usec
,当您需要的时间超过 1 秒时,您需要同时设置两者(但通常最终只设置tv_sec
)The structure is described though:
As you see, the total time is
tv_sec + (1.0/1000000) * tv_usec
seconds. That's why when you need times under a second you settv_usec
, when you need times over 1sec you set both (but usually end up setting onlytv_sec
)tv_sec 将处理整秒,而 tv_usec 处理微秒。
当微秒达到最大值(100 万)时,微秒将重置回 0,并像普通秒表一样增加秒数。
tv_sec will handle the full seconds, while tv_usec handles the microseconds.
The microseconds will reset back to 0 when it get to its max (1 million) and increments the seconds just like a normal stopwatch.
由于我没有设置 tv_usec,所以出现了内存泄漏。 Comuter 获取这两个值的总和,如果您不初始化其中之一,则总和值可能是随机的。
I've had a memory leak because of that I didn't set tv_usec. Comuter takes a sum of those two values and if you not initialize one of them the sum value could be random.