How to use nanosleep() in C? What are `tim.tv_sec` and `tim.tv_nsec`?

CPosixSleep

C Problem Overview


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

C Solutions


Solution 1 - C

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).

Solution 2 - C

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

nanosleep((const struct timespec[]){{0, 500000000L}}, NULL);

Solution 3 - C

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

Solution 4 - C

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

Solution 5 - C

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

Solution 6 - C

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

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

Solution 7 - C

More correct variant:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpnizzleView Question on Stackoverflow
Solution 1 - CNPEView Answer on Stackoverflow
Solution 2 - CDaveView Answer on Stackoverflow
Solution 3 - CglglglView Answer on Stackoverflow
Solution 4 - CSunny ShuklaView Answer on Stackoverflow
Solution 5 - CE.TView Answer on Stackoverflow
Solution 6 - CCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 7 - CRuslan R. LaishevView Answer on Stackoverflow