I saw in usleep man that :
EINVAL
usec is not smaller than 1000000. (On systems where that is considered an error.)
So i wonder if its ok to use usleep in Ubuntu with value greater than 1000000 and if not (or in case that i want to support other platforms ) what is the alternative when i need sleep for 2.2 sec (for example) .
Thank you.
One alternative is to trust the documentation and implement it using a loop just to be safe:
#define USLEEP_MAX (1000000 - 1)
void long_sleep(unsigned long micros)
{
while(micros > 0)
{
const unsigned long chunk = micros > USLEEP_MAX ? USLEEP_MAX : micros;
usleep(chunk);
micros -= chunk;
}
}
You should also inspect the return value of usleep()
, I omitted that for brevity.
In production, you can have fun with Autoconf and friends to detect the proper USLEEP_MAX
at compile-time, and even switch to a plain wrapper if the local system doesn't have a limit for the argument. Hours of fun can be had.
You'd have to look at the Linux kernel source to be 100% sure, but considering that Ubuntu is only distributed for x86 and x86-64, and someone would soon find that sort of behavior unacceptable regardless of whether it's allowed by the underlying POSIX spec, chances are essentially nil that it will ever break.
Linux is ported to a huge variety of systems ports can adopt other ugly preexisting kernel code, so I think the manpage author was just covering his ass when he adapted the POSIX (or whatever) spec which reflects the very loose requirements designed to accommodate odd microcontrollers with slapdash kernels.