87

What is the use of tim.tv_sec and tim.tv_nsec in the following?

How can I sleep execution for 500000 microseconds?

#include <stdio.h>
#include <time.h>

int main()
{
   struct timespec tim, tim2;
   tim.tv_sec = 1;
   tim.tv_nsec = 500;

   if(nanosleep(&tim , &tim2) < 0 )   
   {
      printf("Nano sleep system call failed \n");
      return -1;
   }

   printf("Nano sleep successfull \n");

   return 0;
}
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
pnizzle
  • 6,243
  • 4
  • 52
  • 81

7 Answers7

81

Half a second is 500,000,000 nanoseconds, so your code should read:

tim.tv_sec  = 0;
tim.tv_nsec = 500000000L;

As things stand, you code is sleeping for 1.0000005s (1s + 500ns).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    cool. Just one more question. I am testing this using gettimeofday() twice and getting the difference. I therefore got a difference of half a second. But there was also a very small fraction of a sec, due to cpu processing time assume. How can i calculate this and subtract it from the my sleep time. my time came up as: 0.501033 sec – pnizzle Oct 07 '11 at 07:50
  • 2
    @pnizzle: What exactly are you hoping to achieve with that subtraction? – NPE Oct 07 '11 at 07:51
  • 2
    i am hoping to achieve exactly 0.500000secs. Thanks – pnizzle Oct 07 '11 at 07:54
  • 4
    Most timers aren't that finely grained. Especially on a multitasking machine, it is difficult to sleep precisely. – Dave Oct 07 '11 at 07:56
  • 1
    i have googled a bit and have found a function clock().. What does this do exactly. Does it time cpu usage ? – pnizzle Oct 07 '11 at 08:05
  • @pnizzle: You might consider taking a look here: http://stackoverflow.com/questions/7477486/use-clock-to-count-program-execution-time – alk Oct 07 '11 at 09:42
  • What does the L after `500000000` do? Is it necessary to have that? – HelloGoodbye Oct 14 '15 at 08:58
  • @HelloGoodbye here is the answer to your question: https://stackoverflow.com/questions/10697487/what-does-the-l-at-the-end-of-a-number-do – pnizzle Feb 28 '16 at 22:54
  • for 500,000 microseconds, you can also use usleep(500000), see, https://man7.org/linux/man-pages/man3/usleep.3.html – Rui May 18 '23 at 13:44
59

tv_nsec is the sleep time in nanoseconds. 500000us = 500000000ns, so you want:

nanosleep((const struct timespec[]){{0, 500000000L}}, NULL);
Alex44
  • 3,597
  • 7
  • 39
  • 56
Dave
  • 10,964
  • 3
  • 32
  • 54
  • 2
    What is the ´L` at the end of `500000000L` for? – Sahand Jul 17 '17 at 19:50
  • 3
    This relate to long double. as the second argument of nanosleep() have long double type. see https://stackoverflow.com/questions/1380653/why-do-you-need-to-append-an-l-or-f-after-a-value-assigned-to-a-c-constant – EsmaeelE Aug 05 '17 at 22:12
  • 1
    The type of the second variable of nanosleep structure is `long`. The L is used to convert the number to `long`, cause `int` may be too small to handle the value. Some safety standards (NASA, MISRA) force specifying number type if the number is bigger then 2^16 to avoid buggy code. – KamilCuk Jul 09 '18 at 15:06
12

500000 microseconds are 500000000 nanoseconds. You only wait for 500 ns = 0.5 µs.

glglgl
  • 89,107
  • 13
  • 149
  • 217
10

This worked for me ....

#include <stdio.h>
#include <time.h>   /* Needed for struct timespec */


int mssleep(long miliseconds)
{
   struct timespec rem;
   struct timespec req= {
       (int)(miliseconds / 1000),     /* secs (Must be Non-Negative) */ 
       (miliseconds % 1000) * 1000000 /* nano (Must be in range of 0 to 999999999) */ 
   };

   return nanosleep(&req , &rem);
}

int main()
{
   int ret = mssleep(2500);
   printf("sleep result %d\n",ret);
   return 0;
}
Jonathan
  • 764
  • 6
  • 14
Sunny Shukla
  • 537
  • 1
  • 6
  • 17
8

I usually use some #define and constants to make the calculation easy:

#define NANO_SECOND_MULTIPLIER  1000000  // 1 millisecond = 1,000,000 Nanoseconds
const long INTERVAL_MS = 500 * NANO_SECOND_MULTIPLIER;

Hence my code would look like this:

timespec sleepValue = {0};

sleepValue.tv_nsec = INTERVAL_MS;
nanosleep(&sleepValue, NULL);
E.T
  • 1,095
  • 1
  • 10
  • 19
4

POSIX 7

First find the function: http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html

That contains a link to a time.h, which as a header should be where structs are defined:

The header shall declare the timespec structure, which shall > include at least the following members:

time_t  tv_sec    Seconds. 
long    tv_nsec   Nanoseconds.

man 2 nanosleep

Pseudo-official glibc docs which you should always check for syscalls:

struct timespec {
    time_t tv_sec;        /* seconds */
    long   tv_nsec;       /* nanoseconds */
};
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
4

More correct variant:

{
struct timespec delta = {5 /*secs*/, 135 /*nanosecs*/};
while (nanosleep(&delta, &delta));
}