cusleep

usleep not working, invalid argument error


usleep just isn't waiting any time, the errno variable accuses an invalid argument is being passed on to the function, even if i use the useconds_t type I just get the same error over and over.

I have no idea what's going on here, here's the file I made for some testing.

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

int main() {
    unsigned int delay=500000;
    int err=errno;
    printf("%d\n", err);
    usleep(delay);
    err = errno;
    printf("%d\n", err);
    return 0;
}

strace output:

execve("./a.out", ["./a.out"], 0x7fffec458c30 /* 22 vars */) = 0
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffff9f5d4f0) = -1 EINVAL (Invalid argument)
brk(NULL)                               = 0x1757000
brk(0x17581c0)                          = 0x17581c0
arch_prctl(ARCH_SET_FS, 0x1757880)      = 0
uname({sysname="Linux", nodename="DESKTOP-PRJN4N7", ...}) = 0
readlink("/proc/self/exe", "/mnt/c/Users/User/dmsh/gm/a.out", 4096) = 31
brk(0x17791c0)                          = 0x17791c0
brk(0x177a000)                          = 0x177a000
mprotect(0x4bd000, 12288, PROT_READ)    = 0
fstat(1, {st_mode=S_IFCHR|0660, st_rdev=makedev(0x4, 0x1), ...}) = 0
ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
write(1, "0\n", 20
)                      = 2
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = -1 EINVAL (Invalid argument)
write(1, "22\n", 322
)                     = 3
exit_group(0)                           = ?
+++ exited with 0 +++

Solution

  • readlink("/proc/self/exe", "/mnt/c/Users/User/dmsh/gm/a.out", 4096) = 31

    The path in that line suggests you're using WSL to run your program in Windows, not a straight up Linux or Unix system.

    clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=500000000}, NULL) = -1 EINVAL (Invalid argument)

    And that one tells us you're using a glibc version in WSL that implements usleep() in terms of clock_nanosleep() (My Ubuntu 16.04 WSL installation uses nanosleep(), fwiw. Apparently the glibc people changed the usleep() implementation at some point.)

    The WSL linux emulation layer doesn't support clock_nanosleep() with a CLOCK_REALTIME timer. To verify, this demo program:

    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    int main(void) {
      struct timespec timeout = { .tv_sec = 0, .tv_nsec = 500000000 };
      int err;
      if ((err = clock_nanosleep(CLOCK_REALTIME, 0, &timeout, NULL)) != 0) {    
        printf("clock_nanosleep realtime failed: %s\n", strerror(err));
      } else {
        puts("clock_nanosleep realtime worked.");
      }      
      if ((err = clock_nanosleep(CLOCK_MONOTONIC, 0, &timeout, NULL)) != 0) {
        printf("clock_nanosleep monotonic failed: %s\n", strerror(err));
      } else {
        puts("clock_nanosleep monotonic worked.");
      }
      if (nanosleep(&timeout, NULL) < 0) {
        perror("nanosleep");
      } else {
        puts("nanosleep worked.");
      }
    
      return 0;
    }
    

    should print out

    clock_nanosleep realtime failed: Invalid argument
    clock_nanosleep monotonic worked.
    nanosleep worked.
    

    when you compile and run it.

    The fix is to just not use usleep() (Which is deprecated anyways and no longer in POSIX). Try nanosleep() or the above clock_nanosleep() with a monotonic timer.