开发者

float value in - struct itimerspec

I have this problem: I have created a structure using the itimerspec structure. The itimerspec structure has two fields:

 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 */
       };

So when i am entering:

enter code here
     struct itimerspec its; // argument to timer_gettime
      /* Setting timer interval */

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

      /* Setting timer expiration */
       its.it_value.tv_sec=0.1;  // First expiry after 1 sec
       its.it_value.tv_nsec=0;

     On compilation:
       prototype1.cc:115: warning: converting to ‘__time_t’ from ‘double’

My problem is: as per the design, the user can enter the timer expiration in whole numbers (1, 2, 3 etc which is fine) but can also enter time like 0.1 secs, o.2 secs e开发者_如何学Ctc. But only in seconds.


You will have to adjust to secs and nanosecs, e.g. 0.1 secs = 0secs and 100,000nsecs.


You can't assign a double to a long val. If you need less than 1 sec you should express it in term of usec.

0.1 sec = 100000 usec

Do:

its.it_value.tv_sec=0;  // First expiry after 1 sec
its.it_value.tv_nsec= 0.1 * (usec in sec);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜