0

How can I delay the execution of a program in microseconds? The sleep() function performs a delay in seconds. I also need a delay of less than one second, but at the same time that it was.

#include <stdio.h>
#include <unistd.h>

int main(void) {
    sleep(1);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
12iq
  • 83
  • 5
  • 2
    `usleep()`. But I'm pretty sure this is a duplicate question. – Steve Summit Aug 05 '22 at 19:19
  • 3
    Which platform are you working on? For POSIX, there's [`nanosleep()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html), which allows you to specify the time to nanoseconds. The older function, now deleted from POSIX, is [`usleep()`](https://pubs.opengroup.org/onlinepubs/009695399/functions/usleep.html). – Jonathan Leffler Aug 05 '22 at 19:20
  • 2
    What problem are you trying to solve? How much accuracy do you need? – tadman Aug 05 '22 at 20:06
  • what platform and OS? timing functions are highly HW and OS dependent. Also note that `Sleep/sleep` usually stands for stop the current thread/process/task (uses 0% CPU power) for at least that amount of time but is highly dependent on multitasking OS scheduling granularity so the real time might be much much bigger ... timing functions based on polling (uses 100% CPU power) are usually called `wait,delay` and they usually precise if configured properly, you can even do polling with background work done inside using RDTSC like counters (many CPUs and MCUs have it now) – Spektre Aug 06 '22 at 09:24

0 Answers0