second to nanosecond - struct itimerspec

怎甘沉沦 提交于 2019-12-13 02:15:28

问题


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);
}

回答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.




回答2:


tv_nsec must not be greater than 999,999,999. You are setting it greater than that.



来源:https://stackoverflow.com/questions/5457489/second-to-nanosecond-struct-itimerspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!