秒到纳秒 - struct itimerspec

发布于 2024-10-27 23:00:46 字数 1207 浏览 3 评论 0原文

我正在填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-11-03 23:00:46
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;

将环境变量读取为双精度值。将第二部分存储在 tv_sec 中,将纳秒部分存储在 tv_nsec 中。

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.

风吹短裙飘 2024-11-03 23:00:46

tv_nsec 不得大于 999,999,999。您将其设置得比该值更大。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文