Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to sleep during millisecond

Reply
Thread Tools

How to sleep during millisecond

 
 
Yeounkun, Oh
Guest
Posts: n/a
 
      12-09-2004
Hello.

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

but, how to sleep during milliseconds...

Pls. help me.

Thank you. Regards.


 
Reply With Quote
 
 
 
 
Paul L Daniels
Guest
Posts: n/a
 
      12-09-2004
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


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2004
"Yeounkun, Oh" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      12-09-2004
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

 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      12-09-2004
Thomas Matthews <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      12-09-2004

"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


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      12-09-2004
Merrill & Michele <(E-Mail Removed)> 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 ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      12-10-2004
> 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


 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      12-10-2004
Merrill & Michele <(E-Mail Removed)> 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 ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2004
"Merrill & Michele" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
sleep 0.2 acts more like sleep 1 Richard Ruby 7 05-22-2007 08:02 PM
UTC time in millisecond palmis Java 26 01-19-2006 07:38 AM
pulling my hair out, why won't Kernel.sleep(0) sleep? Sam Roberts Ruby 16 02-18-2005 08:27 PM
What is the difference between Thread.sleep(10) and Thread.currentThread().sleep(10) ? Gonzalo Moreno Java 2 04-05-2004 05:41 PM



Advertisments