第二纳秒 - 结构itimerspec [英] second to nanosecond - struct itimerspec

查看:3934
本文介绍了第二纳秒 - 结构itimerspec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填充timespec结构。其目的是,用户总是会在几秒钟内输入值(也可以是0.01秒),所以我们用转换秒到纳秒: lt_leak_start =环礁(GETENV(LT_LEAK_START))* sec_to_nsec; 其中变量静态长sec_to_nsec = 10亿; ,然后使用它作为参数传递给SETTIME: timer_settime(的timerId,0,和放大器;它的,NULL)。但在做这件事发生错误: SetTimer的失败:无效的参数

请帮我。

先谢谢了。

 在此输入code
 结构的timespec {
    time_t的tv_sec; / *秒*的/
    长tv_nsec; / * *纳秒/
  }; 结构itimerspec {
   结构的timespec it_interval; / *计时器间隔* /
   结构的timespec it_value; / *初始到期* /
 };

在code我试图在这里:

 静态长sec_to_nsec = 10亿;
lt_leak_start =环礁(GETENV(LT_LEAK_START))* sec_to_nsec;/ *设置定时器间隔* /its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 1;/ *设置定时器超时* /its.it_value.tv_sec = 0; 1秒钟后//首先到期
its.it_value.tv_nsec = lt_leak_start;timer_create(CLOCK_REALTIME,&安培; SEVP,&安培;的timerId);如果(timer_settime(的timerId,0,&放大器;它的,NULL)== - 1){
  PERROR(SetTimer的失败);
  出口(1);
}


解决方案

 双D =的strtod(GETENV(LT_LEAK_START),0);
...
its.it_value.tv_sec =(time_t的)D;
its.it_value.tv_nsec =(D - (time_t类型)D)* sec_to_nsec;

阅读环境变量作为双。存储在tv_sec第二部分和tv_nsec纳秒的部分。

i am populating the timespec structure. The intention is, user will always enter values in seconds (can also be 0.01 secs), so we are converting the seconds to nanoseconds using: lt_leak_start = atoll(getenv("LT_LEAK_START")) * sec_to_nsec; where variable static long sec_to_nsec = 1000000000; and then using it as an argument to settime: timer_settime(timerid,0,&its,NULL). But on doing it an error occurs: settimer failed: Invalid argument

Please help me.

Thanks in advance.

enter code here
 struct timespec {            
    time_t tv_sec;    /* Seconds */            
    long   tv_nsec;  /* Nanoseconds */      
  };         

 struct itimerspec {            
   struct timespec it_interval;  /* Timer interval */            
   struct timespec it_value;     /* Initial expiration */        
 }; 

The code i am trying is here:

static long sec_to_nsec = 1000000000;
lt_leak_start = atoll(getenv("LT_LEAK_START")) * sec_to_nsec;

/* Setting timer interval */

its.it_interval.tv_sec=0;
its.it_interval.tv_nsec=1;

/* Setting timer expiration */

its.it_value.tv_sec=0;  // First expiry after 1 sec
its.it_value.tv_nsec=lt_leak_start;

timer_create(CLOCK_REALTIME,&sevp,&timerid);

if(timer_settime(timerid,0,&its,NULL)==-1) {
  perror("settimer failed");
  exit(1);
}

解决方案

double d = strtod(getenv("LT_LEAK_START"), 0);
...
its.it_value.tv_sec=(time_t) d;
its.it_value.tv_nsec=(d - (time_t) d) * sec_to_nsec;

Read the environment variable as a double. Store the second part in tv_sec and the nanosecond part in tv_nsec.

这篇关于第二纳秒 - 结构itimerspec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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