Stories
Slash Boxes
Comments

SoylentNews is people

posted by CoolHand on Monday December 14 2015, @11:35PM   Printer-friendly
from the rock-out-with-your-clock-out dept.

Instructions to sleep for a second almost never result in precisely one second’s sleep. Bob Schmidt walks us through the mechanics of why.

Suppose you are walking down the hallway of your office, and a Summer Intern (SI) intercepts you and asks, “If I put a line of code in my program that simply reads sleep(10) , how long will my program sleep? 1

You look at the harried SI and reply, “It depends,” and you continue on your way.

The SI rushes to catch up with you, and asks, “It depends on what?

And you answer, “That, too, depends,” as you continue walking.

At this point our young SI is frantic (and in immediate danger of going bald). “Stop talking in riddles, grey hair! I’m in real need of help here.

Your stroll has taken you to the entrance of the break room, so you grab your interlocutor, duck inside, grab two cups of your favourite caffeinated beverage, and sit down.

It depends,” you say, “on many things, so let’s start with first things first.

First things first

To understand what’s going on ‘under the hood’ when a sleep() is executed, it helps to know a little about how CPUs work, and that means knowing something about CPU clocks, interrupts, and schedulers. The former two are hardware concepts; the latter is a software concept.

It's a decent peek under the hood for folks who usually treat such things as blackbox.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2) by FatPhil on Tuesday December 15 2015, @08:52AM

    by FatPhil (863) <reversethis-{if.fdsa} {ta} {tnelyos-cp}> on Tuesday December 15 2015, @08:52AM (#276574) Homepage
    "In implementations I’ve seen, the parameter to sleep() – the number of milliseconds to sleep -"

    sleep(3) takes seconds. He's possibly thinking of something like the non-standard msleep(). Which is odd, as he didn't mention msleep() whilst mentioning usleep() and nanosleep() at the start of the article. So clearly he's confused. Confused people shouldn't write articles.
    --
    Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves
    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: -1, Troll) by Anonymous Coward on Tuesday December 15 2015, @09:13AM

    by Anonymous Coward on Tuesday December 15 2015, @09:13AM (#276580)

    For the purposes of this discussion I’m going to assume a generic version of sleep() that accepts a parameter representing time in milliseconds.

    Fat demented idiots shouldn't read articles.

  • (Score: 2) by TheRaven on Tuesday December 15 2015, @11:51AM

    by TheRaven (270) on Tuesday December 15 2015, @11:51AM (#276603) Journal
    He might be thinking of Sleep(), which is the Windows version of msleep().
    --
    sudo mod me up
    • (Score: 2) by FatPhil on Tuesday December 15 2015, @12:35PM

      by FatPhil (863) <reversethis-{if.fdsa} {ta} {tnelyos-cp}> on Tuesday December 15 2015, @12:35PM (#276614) Homepage
      Thanks for the windows insight I was lacking, that makes perfect sense. Linux's msleep() came immediately to mind as I've spent many a year as a kernel programmer, but it's not a c library or posix thing.
      --
      Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves
  • (Score: 2) by sjames on Tuesday December 15 2015, @06:37PM

    by sjames (2882) on Tuesday December 15 2015, @06:37PM (#276748) Journal

    It depends on the language and library you're using. Some sleep functions take ms parameter. which may or may not result in a call to libc's sleep function.