How to sleep during millisecond

Discussion in 'C Programming' started by Yeounkun, Oh, Dec 9, 2004.

  1. Yeounkun, Oh

    Yeounkun, Oh Guest

    Hello.

    Sleep (x) function make a process sleep during x seconds.

    but, how to sleep during milliseconds...

    Pls. help me.

    Thank you. Regards.
    Yeounkun, Oh, Dec 9, 2004
    #1
    1. Advertising

  2. On Thu, 09 Dec 2004 11:40:46 +0900, Yeounkun, Oh wrote:

    > Hello.
    >
    > Sleep (x) function make a process sleep during x seconds.
    >
    > but, how to sleep during milliseconds...


    try 'usleep()'

    Not sure if that'll help.

    --
    PLDaniels - Software - Xamime
    Unix systems Internet Development A.B.N. 19 500 721 806
    PGP Public Key at http://www.pldaniels.com/gpg-keys.pld
    Paul L Daniels, Dec 9, 2004
    #2
    1. Advertising

  3. "Yeounkun, Oh" <> writes:
    > Sleep (x) function make a process sleep during x seconds.
    >
    > but, how to sleep during milliseconds...


    There is no sleep() (or Sleep()) function in standard C, though many
    systems provide such a function. Many systems probably also provide a
    way to sleep for a specified number of milliseconds. You'll need to
    ask in a newsgroup that's specific to your system (Windows, Unix,
    Linux, whatever).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 9, 2004
    #3
  4. Paul L Daniels wrote:
    > On Thu, 09 Dec 2004 11:40:46 +0900, Yeounkun, Oh wrote:
    >
    >
    >>Hello.
    >>
    >>Sleep (x) function make a process sleep during x seconds.
    >>
    >>but, how to sleep during milliseconds...

    >
    >
    > try 'usleep()'
    >
    > Not sure if that'll help.
    >


    Which compiler is usleep() found in?
    I didn't find it in my copy of the C language specification,
    could you let us know where it is?

    Since where off-topic, perhaps you could use:
    wait()
    suspend()
    delay()
    ms_delay()
    us_delay()

    You could also use a timer and poll the "done" bit to
    see when it is finished. Or you could use an interrupt
    system.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Dec 9, 2004
    #4
  5. Yeounkun, Oh

    -berlin.de Guest

    [OT] Re: How to sleep during millisecond

    Thomas Matthews <> wrote:
    > Paul L Daniels wrote:
    >> On Thu, 09 Dec 2004 11:40:46 +0900, Yeounkun, Oh wrote:
    >>>Sleep (x) function make a process sleep during x seconds.
    >>>
    >>>but, how to sleep during milliseconds...

    >>
    >> try 'usleep()'
    >>
    >> Not sure if that'll help.


    > Which compiler is usleep() found in?
    > I didn't find it in my copy of the C language specification,
    > could you let us know where it is?


    > Since where off-topic, perhaps you could use:
    > wait()
    > suspend()
    > delay()
    > ms_delay()
    > us_delay()


    Don't forget about nanosleep(), which is preferable to usleep()
    on UNIX systems...
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Dec 9, 2004
    #5
  6. "Yeounkun, Oh"
    > > Hello.

    >
    > Sleep (x) function make a process sleep during x seconds.
    >
    > but, how to sleep during milliseconds...
    >
    > Pls. help me.
    >
    > Thank you. Regards.


    Jerryrig difftime() found in time.h . If you have a chip made in the last
    fifteen years, I think you can do it in standard C with code that would be
    nonsensical on a slow machine. I think that is far different than
    non-complying or undefined behavior. Google for Chris Torek. MPJ
    Merrill & Michele, Dec 9, 2004
    #6
  7. Yeounkun, Oh

    -berlin.de Guest

    Merrill & Michele <> wrote:

    > "Yeounkun, Oh"
    >> > Hello.

    >>
    >> Sleep (x) function make a process sleep during x seconds.
    >>
    >> but, how to sleep during milliseconds...
    >>
    >> Pls. help me.
    >>
    >> Thank you. Regards.


    > Jerryrig difftime() found in time.h . If you have a chip made in the last
    > fifteen years, I think you can do it in standard C with code that would be
    > nonsensical on a slow machine. I think that is far different than
    > non-complying or undefined behavior. Google for Chris Torek. MPJ


    But there are no guarantees made about the time resolution of difftime()
    (on POSIX systems it's not better than second resolution) and the OP
    wants milliseconds .(And beside that what he wants seems to be to sleep
    and not busy-loop.)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Dec 9, 2004
    #7
  8. > Jens:
    > > Merrill & Michele
    > > >"Yeounkun, Oh"
    > >> > Hello.
    > >>
    > >> Sleep (x) function make a process sleep during x seconds.
    > >>
    > >> but, how to sleep during milliseconds...
    > >>
    > >> Pls. help me.
    > >>
    > >> Thank you. Regards.

    >
    > > Jerryrig difftime() found in time.h . If you have a chip made in the

    last
    > > fifteen years, I think you can do it in standard C with code that would

    be
    > > nonsensical on a slow machine. I think that is far different than
    > > non-complying or undefined behavior. Google for Chris Torek. MPJ

    >
    > But there are no guarantees made about the time resolution of difftime()
    > (on POSIX systems it's not better than second resolution) and the OP
    > wants milliseconds .(And beside that what he wants seems to be to sleep
    > and not busy-loop.)


    To be honest with you, I can't think of a situation makes sense for sleeping
    for a matter of milliseconds. Difftime is guaranteed to return a long in
    seconds. How does a computer distinguish sleep from a busy loop? MPJ
    Merrill & Michele, Dec 10, 2004
    #8
  9. Yeounkun, Oh

    -berlin.de Guest

    Merrill & Michele <> wrote:
    >> Jens:
    >> > Merrill & Michele
    >> > >"Yeounkun, Oh"
    >> >> > Hello.
    >> >>
    >> >> Sleep (x) function make a process sleep during x seconds.
    >> >>
    >> >> but, how to sleep during milliseconds...
    >> >>
    >> >> Pls. help me.
    >> >>
    >> >> Thank you. Regards.

    >>
    >> > Jerryrig difftime() found in time.h . If you have a chip made in the

    > last
    >> > fifteen years, I think you can do it in standard C with code that would

    > be
    >> > nonsensical on a slow machine. I think that is far different than
    >> > non-complying or undefined behavior. Google for Chris Torek. MPJ

    >>
    >> But there are no guarantees made about the time resolution of difftime()
    >> (on POSIX systems it's not better than second resolution) and the OP
    >> wants milliseconds .(And beside that what he wants seems to be to sleep
    >> and not busy-loop.)


    > To be honest with you, I can't think of a situation makes sense for
    > sleeping for a matter of milliseconds.


    When you're dealing with hardware that requires some timeouts in the
    millisecond range it's quite convenient if you don't have to sleep
    each time for at least a second;-) And there are also lots of other
    cases where a sub-second resolution is important.

    > Difftime is guaranteed to return a long in seconds.


    It returns a double. But that still doesn't guarantee even second
    resolution, the standard does not make any statements about the
    time resolution of a time_t, it just states that it's an "arith-
    metic types capable of representing times".

    > How does a computer distinguish sleep from a busy loop? MPJ


    It's heating the CPU less;-) If you're on a multi-tasking system the
    system can schedule other tasks while you're sleeping. Imagine a
    situation where you want to e.g. emit a short beep from the speaker
    every 5th of a second. With busy-looping that task alone would use
    100% CPU time while with a real sleep function it might reduce to a
    few percent - so other jobs could be done in between by the machine.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Dec 10, 2004
    #9
  10. "Merrill & Michele" <> writes:
    [...]
    > To be honest with you, I can't think of a situation makes sense for sleeping
    > for a matter of milliseconds. Difftime is guaranteed to return a long in
    > seconds. How does a computer distinguish sleep from a busy loop? MPJ


    difftime() returns a double, not a long.

    I don't know what you mean by "How does a computer distinguish", but
    there's a major difference in a multiprocessing system. A busy loop
    causes the current program to consume CPU time; a sleep typically
    allows other processes to run. The C standard doesn't deal with
    multiprocessing, but a program (even a strictly conforming one) that
    executes a busy loop is likely to cause problems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 10, 2004
    #10
  11. On Fri, 10 Dec 2004 20:18:53 +0000, Jens.Toerring wrote:

    ....

    >> Difftime is guaranteed to return a long in seconds.

    >
    > It returns a double. But that still doesn't guarantee even second
    > resolution, the standard does not make any statements about the
    > time resolution of a time_t, it just states that it's an "arith-
    > metic types capable of representing times".


    Which is significant because difftime() takes time_t arguments and
    therefore its resolution is limited to that of time_t on that
    implementation. A double return type provides a simple and useful
    interface that can handle finer resolution when it is available.

    Lawrence
    Lawrence Kirby, Dec 13, 2004
    #11
  12. Yeounkun, Oh

    Richard Bos Guest

    -berlin.de wrote:

    > Merrill & Michele <> wrote:
    > > How does a computer distinguish sleep from a busy loop? MPJ

    >
    > It's heating the CPU less;-) If you're on a multi-tasking system the
    > system can schedule other tasks while you're sleeping. Imagine a
    > situation where you want to e.g. emit a short beep from the speaker
    > every 5th of a second. With busy-looping that task alone would use
    > 100% CPU time while with a real sleep function it might reduce to a
    > few percent - so other jobs could be done in between by the machine.


    And let me put on my sysadmin hat once again and add to that that if you
    think you'll get away with using a busy-loop on any of my systems,
    you're sadly mistaken. Hogging resources is a very good reason to ditch
    programs - and programmers.

    Richard
    Richard Bos, Dec 14, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gonzalo Moreno
    Replies:
    2
    Views:
    12,418
  2. palmis

    UTC time in millisecond

    palmis, Jan 10, 2006, in forum: Java
    Replies:
    26
    Views:
    24,856
    Roedy Green
    Jan 19, 2006
  3. Macgyver

    millisecond delay in C - linux

    Macgyver, Jan 7, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,580
    August Derleth
    Jan 7, 2004
  4. Sam Roberts
    Replies:
    16
    Views:
    377
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    228
    Richard
    May 22, 2007
Loading...

Share This Page