8

I was reading a code of application and something caught my attention. The code was : usleep(6*1000*1000). I understand that they use this format for readability issues.

I think that both sleep and usleep use the nanosleep function, so my question is: why not using sleep(6) that does exactly the same thing (ie: sleeps for 6 sec) ? Do we gain in performance when we use usleep ? is usleep more "generic" ?

Loay Ashmawy
  • 677
  • 1
  • 7
  • 26
  • 3
    You should ask the author. – too honest for this site Mar 17 '17 at 15:55
  • https://imgflip.com/i/1llghw – Kerrek SB Mar 17 '17 at 15:59
  • @Olaf - the only answer to this question – KevinDTimm Mar 17 '17 at 15:59
  • 1
    Probably perceived accuracy. Microsleep must have better precision than plain old one second resolution sleep. – Jonathan Leffler Mar 17 '17 at 16:00
  • 2
    man page for `sleep` mentions that it might be implemented using `SIGALRM` so you might not be able to mix calls to `sleep()` with `alarm()` – Chris Turner Mar 17 '17 at 16:02
  • 2
    which `usleep` implementation are you talking about ? [POSIX](http://pubs.opengroup.org/onlinepubs/009695399/functions/usleep.html) only guarantees that it works for values less than 1 million. – Sander De Dycker Mar 17 '17 at 16:07
  • 1
    On some really ugly historical systems, `sleep` was implemented with `alarm` and thus interfered with other use of `SIGALRM` and `alarm`. Perhaps the authors wanted to avoid this. POSIX still (as a bug) permits such implementation, but it's not compatible with implementations which support multithreading, which POSIX also requires, so it should be treated as historical nonsense that's no longer relevant. – R.. GitHub STOP HELPING ICE Mar 17 '17 at 17:13

2 Answers2

11

I think that both sleep and usleep use the nanosleep function,

They may do, or they may not. I'm not aware of any justification in the C and POSIX standards for that supposition.

so my question is: why not using sleep(6) that does exactly the same thing (ie: sleeps for 6 sec) ? Do we gain in performance when we use usleep ? is usleep more "generic" ?

The sleep() function originated in AT&T Unix version 7. The usleep() function originated in BSD 4.3. Although POSIX standardizes a mixture of features drawn from both, there was a time when you were likely to have only one of the two available to you, with which one that was being a function of your particular flavor of Unix.

Nowadays, usleep() is obsolete, and has been removed from POSIX. It's still widely supported, but nanosleep() (or sleep()) should be used instead in new code.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Sleep starts at 1 second and sleeps for years. But is only in increments of whole seconds. usleep is inconveniently defined only for sub-second sleeps, and on most systems will reject any requests above 1.0 sec. nanosleep requires a structure with two slots, the seconds and the subseconds, and so works better for mid-term timing longer than one second. – DragonLord Sep 27 '21 at 16:51
2

The argument to sleep is seconds, the argument to usleep is microseconds. Other than that, I think they're identical.

sleep($n) == usleep($n * 1000000) usleep(25000) only sleeps for 0.025 seconds.

Ashwin Golani
  • 498
  • 3
  • 10
  • Sleep starts at 1 second and sleeps for years. usleep is only for sub-second sleeps, and on most systems will reject any requests above 1.0 sec. – DragonLord Sep 27 '21 at 16:22