设定者问题 [英] setitimer question

查看:49
本文介绍了设定者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在双核计算机上运行以下代码.

I have the following code running on my dual core machine.

当我在同一台PC上运行该应用程序的一个或两个实例时,我具有100毫秒的正确定时分辨率.但是,当我在同一台PC上运行同一应用程序的3个实例时,计时分辨率超过100毫秒.是否有可能使3个应用程序实例以相同的100msec分辨率运行?这与我机器上的内核数量有关吗?

When i'm running one or two instances of the application on the same PC, I have the correct timing resolution of 100msec. However, when i ran 3 instances of the same application on the same PC, the timing resolution is more than 100msec. Is it possible at all to make the 3 instances of application to run with the same resolution of 100msec? Is this related to the number of cores on my machine?

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void timer_handler ( int signum)
{
   double time ; 
   // obtain time here
   gettimeofday() ;
   printf("timer_handler at time = %lf \n",
     time ) ;
}

int main ()
{ 
   struct sigaction sa;
   struct itimerval timer ;

   memset ( &sa, 0, sizeof ( sa ) ) ;

   sa.sa_handler = &timer_handler ;
   sigaction ( SIGALRM, &sa, NULL );

   timer.it_value.tv_sec = 0 ;
   timer.it_value.tv_usec = 100000;
   timer.it_interval.tv_sec = 0;
   timer.it_interval.tv_usec = 100000 ;

   setitimer ( ITIMER_REAL, &timer, NULL ) ;

   for (;;); 
}

推荐答案

手册页 setitimer(2)具有以下内容:

定时器永远不会在请求的时间之前过期,但是可能会在某些时间之前过期此后的(短)时间,这取决于系统计时器的分辨率以及系统负载;参见时间(7).

Timers will never expire before the requested time, but may expire some (short) time afterwards, which depends on the system timer resolution and on the system load; see time(7).

很明显,当您运行更多应用程序实例时,系统负载会增加,计时器的准确性也会降低.

Obviously when you are running more instances of your application the system load will get higher and the timers less accurate.

如果您替换忙碌循环:

for (;;);

使用更少的CPU限制(例如IO限制的工作负载),计时将变得更加准确.

with something less CPU bound (like an IO bound workload) the timing will become more accurate.

这篇关于设定者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆