秒到纳秒 - struct itimerspec
我正在填充 timespec 结构。目的是,用户始终以秒为单位输入值(也可以是 0.01 秒),因此我们使用以下方法将秒转换为纳秒:lt_leak_start = atoll(getenv("LT_LEAK_START")) * sec_to_nsec;
其中变量static long sec_to_nsec = 1000000000;
,然后将其用作settime的参数:timer_settime(timerid,0,&its,NULL)
。但执行此操作时会发生错误:settimer failed: Invalid argument
请帮助我。
提前致谢。
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 */
};
我正在尝试的代码在这里:
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将环境变量读取为双精度值。将第二部分存储在 tv_sec 中,将纳秒部分存储在 tv_nsec 中。
Read the environment variable as a double. Store the second part in tv_sec and the nanosecond part in tv_nsec.
tv_nsec
不得大于 999,999,999。您将其设置得比该值更大。tv_nsec
must not be greater than 999,999,999. You are setting it greater than that.