Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sleep less than 1 second

570 views
Skip to first unread message

Dotan Halevi

unread,
Jan 22, 2003, 4:20:54 AM1/22/03
to
in Linux,
how can I "sleep" for less than 1 second ?
a milisecond resulution is OK.


Thanks, Dotan

Wilfried

unread,
Jan 22, 2003, 5:29:56 AM1/22/03
to
Dotan Halevi wrote:

You can try to write a small Tcl-script using Tcl_Sleep. Tcl_Sleep can work
with miliseconds.

Stephane CHAZELAS

unread,
Jan 22, 2003, 5:39:50 AM1/22/03
to
On Wed, 22 Jan 2003 11:20:54 +0200, Dotan Halevi <dha...@iil.intel.com> wrote:
> in Linux,
> how can I "sleep" for less than 1 second ?
> a milisecond resulution is OK.

With latest version of zsh, you can have a float type SECONDS
variable.

typeset -F SECONDS
SECONDS=0

while (( SECONDS < 0.1 )); do
whatever
done

Of course
while (( SECONDS < 0.1 )); do
:
done

is not a good approach for just sleeping as it eats all CPU
resources but that's the most accurate you can get in a shell
script.

It's likely that you need a real programming language here.

--
Stéphane

John L

unread,
Jan 22, 2003, 9:20:18 AM1/22/03
to

"Stephane CHAZELAS" <stephane...@yahoo.fr> wrote in message news:slrnb2srdk.8t.s...@pcchazelas.free.fr...

Yes. It is tempting to write a C or perl program which would
sleep for the requisite fraction of a second, but if you tried to
call it from a shellscript, the start-up time (to load the program)
would be too great compared to the sleep time wanted. You could,
presumably, make sure it is already memory-resident when you
want to call it. (Or you could hack your shell to use your own
sleep function.)

Then again, millisecond resolution, which the OP wants, might
well depend on what other work his computer is doing.

John.


Dotan Halevi

unread,
Jan 22, 2003, 9:10:24 AM1/22/03
to
Wilfried & Stephane,
Thanks for the prompt answers.

I have poked a little further and came up w/ these solutions that works on
RedHat 7.2

1. use usleep - a binary that will sleep in micro second resolution (might
not be avaiable on other Unix-s)
2. define this function (bash)
function msleep() { tcl -c "after $*" ; }
Note: "after" will not work on old (ver ?) tcl interpreters, so aain, it
might not work on other Unix-s.
3. write the msleep function yourself, using the C usleep function:

______________ start of msleep C program _____________
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
long time;
time = atol(argv[1]) * 1000;
usleep(time);
}
_____________ end ___________________________________


Dotan

"Dotan Halevi" <dha...@iil.intel.com> wrote in message
news:b0lnpn$b0v$1...@news01.intel.com...

Dan Mercer

unread,
Jan 22, 2003, 12:22:52 PM1/22/03
to
In article <b0lnpn$b0v$1...@news01.intel.com>,
Dotan Halevi <dha...@iil.intel.com> writes:
> in Linux,
^^^^^

> how can I "sleep" for less than 1 second ?
> a milisecond resulution is OK.
>
>
> Thanks, Dotan
>

If you have ksh93, the builtin sleep command takes decimal fractions:

$ SECONDS=0;sleep .5;echo $SECONDS
0.506

Otherwise use usleep which sleeps microseconds:

$ SECONDS=0;usleep 500000;echo $SECONDS
0.511


--
Dan Mercer
dame...@mmm.com

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

Opinions expressed here are my own and may not represent those of my employer.

John DuBois

unread,
Jan 23, 2003, 2:33:13 AM1/23/03
to
In article <b0m95j$ptt$1...@news8.svr.pol.co.uk>,

John L <j...@lammtarra.fslife.co.uk> wrote:
>
>> On Wed, 22 Jan 2003 11:20:54 +0200, Dotan Halevi <dha...@iil.intel.com> wrote:
>> > in Linux,
>> > how can I "sleep" for less than 1 second ?
>> > a milisecond resulution is OK.
>>
>
>Yes. It is tempting to write a C or perl program which would
>sleep for the requisite fraction of a second, but if you tried to
>call it from a shellscript, the start-up time (to load the program)
>would be too great compared to the sleep time wanted.

To get millisecond resolution, perhaps. But it isn't clear whether the OP
actually needs ms resolution, or whether something greater than that (but
less than 1s) would also be "OK".

$ a=$SECONDS; timer -r1 -s '' 100ms; b=$SECONDS; print $a $b
75748.464 75748.579

So, I got 115ms instead of 100ms (this is a busy system).

Another approach:

function pause {
print -p $1
read -p
}
timer |&

a=$SECONDS; pause 100ms; b=$SECONDS; print $a $b
22.502 22.602

ftp://ftp.armory.com/pub/source/timer.tar.gz

John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

Tim Cargile

unread,
Jan 23, 2003, 7:19:16 AM1/23/03
to
spc...@deeptht.armory.com (John DuBois) wrote in message news:<3e2f9ab8$0$79563$8ee...@newsreader.tycho.net>...

> In article <b0m95j$ptt$1...@news8.svr.pol.co.uk>,
> John L <j...@lammtarra.fslife.co.uk> wrote:
> >
> >> On Wed, 22 Jan 2003 11:20:54 +0200, Dotan Halevi <dha...@iil.intel.com> wrote:
> >> > in Linux,
> >> > how can I "sleep" for less than 1 second ?
> >> > a milisecond resulution is OK.
> >>
> >
> >Yes. It is tempting to write a C or perl program which would
>
snip
>
> ftp://ftp.armory.com/pub/source/timer.tar.gz
>
> John

I started porting 'timer' to Cygwin and ran into problems with 'errno',
which is implemented as a function rather than a global.
I finally got a clean compile/link by faking errno, but it
suspended when it ran. Any advice?

TIA

Tim

John DuBois

unread,
Jan 23, 2003, 7:38:22 PM1/23/03
to
In article <8b67aef5.03012...@posting.google.com>,
Tim Cargile <teca...@hotmail.com> wrote:
>>
>> ftp://ftp.armory.com/pub/source/timer.tar.gz

>
>I finally got a clean compile/link by faking errno, but it
>suspended when it ran. Any advice?

Did you give it any arguments? If you don't specify a time period on the
command line, it will read time periods from its standard input (and wait
indefinitely for such).

If you did give it correct arguments, it may be that some Cygwin functionality
is broken. timer can use either usleep or itimers (see the Makefile). Under
some OSes, these may be implemented using a common resource, but whichever
you're using it would be worth trying the other.

Tim Cargile

unread,
Jan 24, 2003, 1:51:56 AM1/24/03
to
Stephane CHAZELAS <stephane...@yahoo.fr> wrote in message news:<slrnb2srdk.8t.s...@pcchazelas.free.fr>...
> On Wed, 22 Jan 2003 11:20:54 +0200, Dotan Halevi <dha...@iil.intel.com> wrote:
> > in Linux,
> > how can I "sleep" for less than 1 second ?
> > a milisecond resulution is OK.
>
> With latest version of zsh, you can have a float type SECONDS
> variable.
>
> typeset -F SECONDS
> SECONDS=0
>
> while (( SECONDS < 0.1 )); do
> whatever
> done
>
...

I'm running zsh 4.0.6 (which I recently installed on Cygwin),
which seems to be current according to the 'zsh.org' site.
With this release, I can't 'typeset-F SECONDS' because it's a
'special' parameter. Also, it seems to be an integer through timing
tests that
compared it a 'type -F' variable that had been initialized to
fractional
second values:

typeset -F timer

SECONDS=0
timer=4.5

while (( SECONDS <= $timer )); do
:
done

With this, I get indications, that I can't get second fractional
granularity in the run times. In fact, the timings vary at least
500 msec. from run to run.

OTH, using the 'msleep' 'C' module (submitted in this thread)
via a bash script, I can get excellent msec. granularity with
repeatibility. I can measure the process startup overhead of
my entire 'msleep' test sequence as a very repeatable 100 msec
(using the 'time' command for all tests).

What am I doing wrong here? Is there a more current version of
'zsh' than 4.0.6 that has the 'special' SECONDS parameter internally
typeset as -F?

Tim

Stephane CHAZELAS

unread,
Jan 24, 2003, 3:46:08 AM1/24/03
to
On 23 Jan 2003 22:51:56 -0800, Tim Cargile <teca...@hotmail.com> wrote:
[...]

> What am I doing wrong here? Is there a more current version of
> 'zsh' than 4.0.6 that has the 'special' SECONDS parameter internally
> typeset as -F?

From zsh ChangeLog:

2002-10-29 Peter Stephenson <p...@csr.com>

* 17868: Src/builtin.c, Src/params.c, Doc/Zsh/params.yo:
Can `typeset -F SECONDS' to get better accuracy. Try to
catch all cases when converting or creating local copy
(but I missed at least one --- next patch).

It's in 4.1.x (developpement series).

You can also write a module for zsh with nanosleep.
See Src/Modules/stat.* for a simple example.

--
Stéphane

Tim Cargile

unread,
Jan 24, 2003, 5:47:49 AM1/24/03
to
spc...@deeptht.armory.com (John DuBois) wrote in message news:<3e308afe$0$79564$8ee...@newsreader.tycho.net>...

> In article <8b67aef5.03012...@posting.google.com>,
> Tim Cargile <teca...@hotmail.com> wrote:
> >>
> >> ftp://ftp.armory.com/pub/source/timer.tar.gz
> >
> >I finally got a clean compile/link by faking errno, but it
> >suspended when it ran. Any advice?
>
> Did you give it any arguments? If you don't specify a time period on the
> command line, it will read time periods from its standard input (and wait
> indefinitely for such).

My bad. I specified -r1 -s '' 100ms. I wasn't thinking (nor reading
your very good source code comments). The Cygwin port I did was a kludge
that works OK as long as there are not any errors to report, that is.


>
> If you did give it correct arguments, it may be that some Cygwin functionality
> is broken. timer can use either usleep or itimers (see the Makefile). Under
> some OSes, these may be implemented using a common resource, but whichever
> you're using it would be worth trying the other.

For a plain msec wait I'm using this (submitted by Dotan Halevi)
which compiled/ran fine with Cygin:

#include <unistd.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
long time;
time = atol(argv[1]) * 1000;
usleep(time);
}

Thanks Again and I apologize for the inconvenience to you.
Wonder if I should do a 'proper' port of 'timer' to Cygwin ...
>
> John

Chris F.A. Johnson

unread,
Jan 24, 2003, 3:19:14 PM1/24/03
to
On Wed, 22 Jan 2003 at 09:20 GMT, Dotan Halevi wrote:
> in Linux,
> how can I "sleep" for less than 1 second ?
> a milisecond resulution is OK.

The latest version of GNU sleep (in sh-utils-2.0.15) takes a
floating point argument. I couldn't get it to compile on SunOS
4.1.4, though.

--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

0 new messages