Thanks, Dotan
You can try to write a small Tcl-script using Tcl_Sleep. Tcl_Sleep can work
with miliseconds.
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
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.
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...
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.
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/
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
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.
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
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
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
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