Thread: sleep(1) takes longer each time it is called?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    sleep(1) takes longer each time it is called?

    When I call sleep() and check gettimeofday before and after, it sleeps longer each time I call it. Does anyone know of a way to sleep for a set amount of real time?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no way, unless you have a real-time OS.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    This is OS dependent. On Linux, you can use nanosleep() to get the job done. It has a bit more guarantees that sleep().

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can never get your OS to sleep for exact n seconds unless you have a real-time OS. You can come close, though.
    A scheme is to time how long your sleeps take and adjust the following sleeps in order to achieve "semi-accurate" sleep for n seconds. It's the best you can do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I also tried nanosleep with similar increases every time its called:

    Code:
    struct timespec in, out;
    in.tv_sec = 0;
    in.tv_nsec = 1;
    
    nanosleep(&in, &out);
    I was hoping this would sleep somewhere near 1 nanosecond and the first time it is only a few hundred nanoseconds but then the second time its up to hundreds of microseconds...

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    According to this theory, then after a sufficient number of calls it will be sleeping for hours when you ask for 1 nanosecond. I simply don't believe that. How many measurements did you take? I think you're seeing a random trend that means nothing.

    Take ten thousand measurements and see if the trend continues. If it does, something is really whacked with your system.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think you'll ever get it to sleep for 1 nano-second (unless you've got some kind of liquid-nitrogen cooled supercomputer). Most CPU's run at <= 3GHz. Most CPU instructions would take longer than 3 clock ticks, and you'd need a lot of instructions to do a context switch, execute sleep and then context switch again.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I don't think you'll ever get it to sleep for 1 nano-second (unless you've got some kind of liquid-nitrogen cooled supercomputer). Most CPU's run at <= 3GHz. Most CPU instructions would take longer than 3 clock ticks, and you'd need a lot of instructions to do a context switch, execute sleep and then context switch again.
    I think his concern is that it seems to take longer each time he calls it... which is almost certainly an illusion.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I wrote a piece of code that pauses execution for a given amount of seconds. It's at my home computer, I will post it later.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Babkockdood View Post
    I wrote a piece of code that pauses execution for a given amount of seconds. It's at my home computer, I will post it later.
    And that will probably also appear to sleep for longer each time it is called, quite simply because his timing code is probably wrong.

    lsad, show how you are timing these.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    #include <time.h>
    
    wait(float x)
    {
    	time_t start;
    	time_t current;
    	time(&start);
    	do
    		time(&current);
    	while(difftime(current,start) < x);
    }
    This always works for me.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Babkockdood View Post
    Code:
    #include <time.h>
    
    wait(float x)
    {
    	time_t start;
    	time_t current;
    	time(&start);
    	do
    		time(&current);
    	while(difftime(current,start) < x);
    }
    This always works for me.
    It's also greedy and naive.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It may surprise you how sleep(N) works. It doesn't try to sleep for N amount of time, instead it sleeps for AT LEAST N amount of time, and then returns.

    Which means that, with a busy multi-tasking computer, you could have a good deal more sleep time, than you requested, AND in any case, you will ALWAYS lose a tiny amount of time. Even on a computer running no other (non OS) applications.

    If you put your sleep() calls, into a loop, you should see some increase in lost time, simply because you have added to the work load of the OS. If the load lightens, the sleep times will get trimmed back down.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Babkockdood View Post
    Code:
    #include <time.h>
    
    wait(float x)
    {
    	time_t start;
    	time_t current;
    	time(&start);
    	do
    		time(&current);
    	while(difftime(current,start) < x);
    }
    This always works for me.
    That is a busy loop, not a sleep. CPU usage will be maxed out by executing that code. Using such code for a delay of a second is downright cruel!
    That technique is only meant for spin-locks where the duration is expected to be typically perhaps less than tenths of a millisecond.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  2. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  3. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  4. time a function takes to execute (up to the millisecond)
    By jszielenski in forum C Programming
    Replies: 4
    Last Post: 11-23-2001, 01:34 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

Tags for this Thread