Is there an alternative sleep function in C to milliseconds?

I have the source code that was compiled on Windows. I am converting it to Red Hat Linux.

The source code includes the <windows.h> header file, and the programmer used the Sleep() function to wait for a period in milliseconds. This will not work on Linux.

However, I can use the sleep(seconds) function, but it uses an integer in seconds. I do not want to convert milliseconds to seconds. Is there an alternative sleep function that I can use with gcc compilation on Linux?

+116
c linux sleep
Jul 21 '09 at 3:49
source share
6 answers

Yes - older POSIX standards are defined by usleep() , so this is available on Linux:

  int usleep(useconds_t usec); 

DESCRIPTION

The usleep () function pauses the execution thread for (at least) microseconds. Sleep can be slightly lengthened by any system activity or the time spent processing a call, or the granularity of system timers.

usleep() takes microseconds , so you have to multiply your input by 1000 to sleep in milliseconds.




usleep() has since been deprecated and subsequently removed from POSIX; nanosleep() preferred for new code:

  #include <time.h> int nanosleep(const struct timespec *req, struct timespec *rem); 

DESCRIPTION

nanosleep() pauses the execution of the calling thread until at least the time specified in *req expires, or until the signal that starts the handler call to the calling thread or terminates the process.

The timepec structure is used to specify time intervals accurate to the nanosecond. It is defined as follows:

  struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; 

An example of the msleep() function, implemented using nanosleep() , which continues to sleep mode if it is interrupted by a signal:

 int msleep(long msec) { struct timespec ts; int res; if (msec < 0) { errno = EINVAL; return -1; } ts.tv_sec = msec / 1000; ts.tv_nsec = (msec % 1000) * 1000000; do { res = nanosleep(&ts, &ts); } while (res && errno == EINTR); return res; } 
+160
Jul 21 '09 at 3:52
source share

You can use this cross-platform feature:

 #ifdef WIN32 #include <windows.h> #elif _POSIX_C_SOURCE >= 199309L #include <time.h> // for nanosleep #else #include <unistd.h> // for usleep #endif void sleep_ms(int milliseconds) // cross-platform sleep function { #ifdef WIN32 Sleep(milliseconds); #elif _POSIX_C_SOURCE >= 199309L struct timespec ts; ts.tv_sec = milliseconds / 1000; ts.tv_nsec = (milliseconds % 1000) * 1000000; nanosleep(&ts, NULL); #else usleep(milliseconds * 1000); #endif } 
+41
Mar 03 '15 at 8:30
source share

As an alternative to usleep() , which was not defined in POSIX 2008 (although it was defined before POSIX 2004, and it is obviously available on Linux and other platforms with a POSIX compliance history), the POSIX 2008 standard defines nanosleep() :

nanosleep - high resolution

 #include <time.h> int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); 

The nanosleep() function must suspend the current thread from execution until the time interval specified by the rqtp argument is rqtp or the signal is passed to the calling thread and its action calls the signal capture function or terminate the process. The pause time may be longer than requested because the value of the argument is rounded to an integer multiple of the resolution of sleep, or due to the planning of other activities by the system. But, except in the case of a signal interruption, the suspension time should not be less than the time specified by rqtp , as measured by the CLOCK_REALTIME system clock.

Using the nanosleep() function does not affect the action or blocking of any signal.

+31
Jul 21 '09 at 4:02
source share

Outside of usleep , a modest select with NULL file descriptor sets will allow you to pause accurate to the microsecond and without the risk of SIGALRM complications.

sigtimedwait and sigwaitinfo offer similar behavior.

+24
Jul 21 '09 at 3:58
source share
 #include <unistd.h> int usleep(useconds_t useconds); //pass in microseconds 
+13
Jul 21 '09 at 3:58
source share
 #include <stdio.h> #include <stdlib.h> int main () { puts("Program Will Sleep For 2 Seconds"); system("sleep 2"); // works for linux systems return 0; } 
-6
Aug 09 '18 at 17:52
source share



All Articles