How to use nanosleep() in C? What are `tim.tv_sec` and `tim.tv_nsec`?
Solution 1:
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:
tv_nsec
is the sleep time in nanoseconds. 500000us = 500000000ns, so you want:
nanosleep((const struct timespec[]){{0, 500000000L}}, NULL);
Solution 3:
500000 microseconds are 500000000 nanoseconds. You only wait for 500 ns = 0.5 µs.