Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HouzuoGuo/31e640d4f61229f3e76853bfa4537b05 to your computer and use it in GitHub Desktop.
Save HouzuoGuo/31e640d4f61229f3e76853bfa4537b05 to your computer and use it in GitHub Desktop.
Fix all sorts of timer and sleep issues for Ubuntu 20.04 running in WSL
Solution is taken from: https://github.com/microsoft/WSL/issues/4898#issuecomment-646790723
cat <<'EOF' > nanosleep.c
#include <time.h>
#include <unistd.h>
int nanosleep(const struct timespec *req, struct timespec *rem)
{
return clock_nanosleep(CLOCK_MONOTONIC, 0, req, rem);
}
int usleep(useconds_t usec)
{
struct timespec req = {
.tv_sec = (usec / 1000000),
.tv_nsec = (usec % 1000000) * 1000,
};
return nanosleep(&req, NULL);
}
EOF
gcc -shared -fPIC -o /usr/local/lib/libnanosleep.so nanosleep.c
echo '/usr/local/lib/libnanosleep.so' >> /etc/ld.so.preload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment