Thread: sleeping for some seconds

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    33

    sleeping for some seconds

    Hello!

    I'm writing a program that have to write every 1.654 seconds (this number is random). The real time I have to wait is calculated and safe in a double type variable. Does anybody know how to make it? I've tried with nanosleep and usleep. But when I try to handle the time variables all goes crazy! Thank for your help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    "the time variables all goes crazy" means what?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So I presume you are doing this in Linux.

    You are, I presume, aware that Linux is not a real-time OS, and thus makes no express or implied warranty with regards to it's "timeliness" - usleep and nanosleep are both capable of sleeping for a set amount of time, but it's a "best effort", and although the names imply that they are micro or nanosecond precise, they are not guaranteed to be very precise [how good or bad they are will depend partly on what hardware you are doing this on].

    The other thing I'm not clear on is your statement "has to write every X seconds" - what are yout "writing" and where is that write intended for? If it's a write to for example disk or network, there's absolutely no guarantee that it will actually get done any time real soon after you issuing the write from your application. Obviously a write to a hardware register will be much more direct.

    It may be easier to explain what you may need to do or what you can't do if you post a small example of what you're trying to do...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Most implementations of sleep/delay/whatever only guarantee a MINIMUM time, that is AT LEAST the amount you request for.

    Asking to sleep for 1 second, but coming back 20 seconds later is within spec (maybe that is not what you want).

    Unless you're programming on a machine where you have absolute control over everything (a standard desktop machine with any popular OS doesn't count), getting absolute timings is going to be tricky at best.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    I'll try to explain better. Sorry.

    I want to make a program that every 0.5 secs makes a printf("It has passed 0.5 sec"). I don't mind if the precision isn't very good.

    The problem with the time variables is that I don't know wich values gives to a variable that is of time_t for example.

    I think of using a solution like these one:
    Code:
    int time_sec;
    int time_usec;
    
    time_sec = 0;
    time_usec = 500000;
    
    while(TRUE){
    
    sleep(time_sec);
    usleep(time_usec);
    printf("It has passed %f sec\n",time_sec+time_usec*1e-6);
    
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what you mean you want to do with time_t, and how that relates to your code.

    usleep and sleep are both (generally) implemented as calls to nanosleep with a suitably multiplied input amount of time. (e.g. usleep is essentially nanosleep(time * 1000) [yes, I know, that's not the right way to call nanosleep].

    So calling sleep followed by usleep won't really make much sense.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Quote Originally Posted by matsp View Post
    Not sure what you mean you want to do with time_t, and how that relates to your code.
    That one was for McGyver As you say useless in my code.



    usleep and sleep are both (generally) implemented as calls to nanosleep with a suitably multiplied input amount of time. (e.g. usleep is essentially nanosleep(time * 1000) [yes, I know, that's not the right way to call nanosleep].

    Quote Originally Posted by matsp View Post
    So calling sleep followed by usleep won't really make much sense.
    In this case maybe not, but what if you want to make a 1.25 second sleep? usleep uses usecond_t variables. And the type useconds_t is an unsigned integral capable of storing values at least in the range zero to 1,000,000 that's from 0 to 1 sec I think.

    --
    Mats[/QUOTE]

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The sleep() family of functions have nothing to do with time_t.

    So....

    What is the issue again?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to sleep 1.25 seconds, you specify 1250000 ((int)(1.25 * 1e6) if you want it as a calculation, or 1250 * 1000 for integer, where 1250 is the number of milliseconds you want to sleep) to usleep. useconds_t is certainly a 32-bit integer if it's not larger, so you should be fine up to about 2000 seconds at least.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by bartleby84 View Post
    That one was for McGyver As you say useless in my code.



    usleep and sleep are both (generally) implemented as calls to nanosleep with a suitably multiplied input amount of time. (e.g. usleep is essentially nanosleep(time * 1000) [yes, I know, that's not the right way to call nanosleep].



    In this case maybe not, but what if you want to make a 1.25 second sleep? usleep uses usecond_t variables. And the type useconds_t is an unsigned integral capable of storing values at least in the range zero to 1,000,000 that's from 0 to 1 sec I think.

    --
    Mats
    [/QUOTE]

    If you wanted a 1.25 second sleep, you'd call sleep with 1250 as an argument, clearly.. One second = 1000 milliseconds.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you wanted a 1.25 second sleep, you'd call sleep with 1250 as an argument, clearly.. One second = 1000 milliseconds.[/QUOTE]

    usleep takes a number of microseconds, so passing it that type of value is probably best, hence my suggestion of using milliseconds * 1000.

    sleep (in Linux) sleeps for a number of seconds.

    Both are different from Sleep in Windows and delay in Borland/TurboC that uses millieseconds as their inputs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Quote Originally Posted by MacGyver View Post
    The sleep() family of functions have nothing to do with time_t.

    So....

    What is the issue again?
    I first tried to use the nanoleep() function that need a timespec struture and this structure has two variables, one is a long to represent the nanosecs (from 0 to 1 sec) an the other one to represent the secs is a time_t variable.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    Quote Originally Posted by matsp View Post
    If you want to sleep 1.25 seconds, you specify 1250000 ((int)(1.25 * 1e6) if you want it as a calculation, or 1250 * 1000 for integer, where 1250 is the number of milliseconds you want to sleep) to usleep. useconds_t is certainly a 32-bit integer if it's not larger, so you should be fine up to about 2000 seconds at least.

    --
    Mats
    Thats is new for me I read from http://www.opengroup.org/onlinepubs/...s/types.h.html that useconds_t has a limited range. There was my problem.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    From your source:

    The type useconds_t shall be an unsigned integer type capable of storing values at least in the range [0, 1000000].
    I strongly doubt that it would not support a value of 1,250,000 on your architecture, but whatever.

  15. #15
    Registered User
    Join Date
    Mar 2007
    Posts
    33
    I tried to make a usleep(2000000) and it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reboot(), takes a few seconds before rebooting
    By galapogos in forum Linux Programming
    Replies: 5
    Last Post: 12-01-2008, 01:21 PM
  2. How to build a timer that counts the seconds elapsed?
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-28-2007, 12:26 PM
  3. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  4. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  5. Extra Sleeping Info...
    By ale_jrb in forum C++ Programming
    Replies: 11
    Last Post: 05-15-2004, 08:15 AM