Can Unix sleep in terms of mili/micro?

Can Unix sleep in terms of mili/micro?

Post by TAYBENGH%NUSDISCS.BIT.. » Tue, 11 Sep 1990 14:03:30



        Can Unix sleep in terms of mili or mirco second? I am aware that
sleep() can only sleep in terms of second. Please specify the Unix Dialect
when u reply. Thanks.


 
 
 

Can Unix sleep in terms of mili/micro?

Post by Tony » Tue, 11 Sep 1990 15:43:42


            Can Unix sleep in terms of mili or mirco second? I am aware that
    sleep() can only sleep in terms of second. Please specify the Unix Dialect
    when u reply. Thanks.

BSD derived systems provide usleep(3) which does what you want.  

--
Tony Li - USC Computer Science Department

Thus spake the master programmer: "A well written program is its own
heaven; a poorly-written program its own hell."

 
 
 

Can Unix sleep in terms of mili/micro?

Post by 2307GILLV%MUCSD.BIT.. » Tue, 11 Sep 1990 23:31:51


The nap () system all in Xenix 2.3.2 can sleep in terms of
milli seconds.   Caveat Emptor This function is linked with
the system clock, which is, in most has a granularity of
tens of milli seconds.

        -I compute, Therefore I VAX
        -Dicky Gill at MUCSD.EDU

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Tom Weinste » Tue, 11 Sep 1990 23:38:44




>             Can Unix sleep in terms of mili or mirco second? I am aware that
>     sleep() can only sleep in terms of second. Please specify the Unix Dialect
>     when u reply. Thanks.

> BSD derived systems provide usleep(3) which does what you want.  

Yuck.  Ick.  Ptui.

usleep is really pretty icky because it uses itimers, and the system
call overhead (4 of them!) can seriously destroy the accuracy of your
timing.  A much better idea is to use something like this:

{
   static struct timeval tv;

   tv.tv_sec = usecs / 1000000;
   tv.tv_usec = usecs % 1000000;

   select(0, 0, 0, 0, &tv);

Quote:}

This uses only one system call instead of the 4 of usleep, and it
doesn't use itimers so it won't*up programs that use them for
other things.

--
Tom Weinstein
Silicon Graphics, Inc., Entry Systems Division, Window Systems

Any opinions expressed above are mine, not sgi's.

 
 
 

Can Unix sleep in terms of mili/micro?

Post by franck boissie » Wed, 12 Sep 1990 00:45:46



Quote:

> BSD derived systems provide usleep(3) which does what you want.  

On some manuals for usleep it is mentionned that you may use select
with no descriptor to use to do the trick.
--



35512 CESSON SEVIGNE CEDEX  FRANCE    
 
 
 

Can Unix sleep in terms of mili/micro?

Post by Robert Din » Wed, 12 Sep 1990 04:38:48



>         Can Unix sleep in terms of mili or mirco second? I am aware that
> sleep() can only sleep in terms of second. Please specify the Unix Dialect
> when u reply. Thanks.



     Two provisions I'm familiar with:

     Under Xenix there is a nap() system call. It takes an argument in
milliseconds, but the granularity depends on the system tick rate.
To use it the -lx (to link the xenix library) is required.

     Under Sun OS, usleep(), which takes an argument in microseconds
is available. I don't remember having to do anything special to use it but
it's been awhile.

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Cheung Yukn » Wed, 12 Sep 1990 17:55:26


I remember reading somewhere that the select() function can be used to
do this. Anybody knows exactly how it is done? Thanks.
 
 
 

Can Unix sleep in terms of mili/micro?

Post by Michael Morre » Fri, 14 Sep 1990 06:56:23



Quote:>>        Can Unix sleep in terms of mili or mirco second?
> sleep_less_than_sec(x)
> int x;
> {
>    int i;

>    for (i = 0;i <= x;i++)
>            ;
> }
>    I'd think that would give you approx. 25 clock cycles, dependent on
>the machine, the load on the machine, etc.

----------

This is not a very good idea.  Any decent optimizer will simply optimize away
your code loop and you won't get any delay (although if you declare "i" as
volatile, it shouldn't be removed, but it's still very dependent on the
machine and its current load).

  Michael

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Sandeep Meh » Fri, 14 Sep 1990 22:36:08




      >
      >        Can Unix sleep in terms of mili or mirco second? I am aware that
      >sleep() can only sleep in terms of second. Please specify the Unix Dialect
      >when u reply. Thanks.

              How 'bout:

      sleep_less_than_sec(x)
      int x;
      {
              int i;

              for (i = 0;i <= x;i++)
                      ;
      }

For many (obvious) reasons a dummy loop is not the best idea. Besides
you may not have a real time clock in your UNIX box, e.g., Sun 3's (at
least the ones I use) use the Intersil 7170 "real-time" clock in 100 Hz
periodic mode, giving a resolution of only 10 milliseconds. Using the
clock(3C) function yields CPU time used only up to a 16.667 milliseconds
accuracy.  Sun 3 (SunOS 4.0) getrusage() assumes tick intervals of
20msec, for Sun 4's 10msec. I know that struct timerval' has a "usec"
field but of course the value is hardware dependent.

You might write your own little assembler routine to use a spare counter
or something (not advisable :-)) which will suffer from all the

Berkeley have a real-time timer facility for Sun 3's under SunOS and
it's (h'ware + s'ware) pretty cheap.  But then again, is UNIX/SunOS
real-time ?

Don't know if I answered the question either ?

sandeep
--

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Conor P. Cahi » Fri, 14 Sep 1990 08:13:26




>Looping is in general a bad delay because different machines require
>different CPU time to perform the loop. Intelligent use of macros can
>help this, but unless it is in the kernel where a context switch
>cannot occur you can only guareentee that you will not return before
>the required delay.

Plus it has a bad little side effect of eating up gobs and gobs of cpu time.

--
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170

 
 
 

Can Unix sleep in terms of mili/micro?

Post by V. S. Subrahmany » Sat, 15 Sep 1990 15:11:00




>>>        Can Unix sleep in terms of mili or mirco second?

>> sleep_less_than_sec(x)
>> int x;
>> }
>----------

>This is not a very good idea.  Any decent optimizer will simply optimize away
>your code loop and you won't get any delay (although if you declare "i" as
>volatile, it shouldn't be removed, but it's still very dependent on the
>machine and its current load).

>  Michael

You can try using "blocking I/O" :

        1. Select a tty line that will not be used by any other person.
        2. Open the tty line for read
        3. ioctl that tty line , with vtime field in the c_cc array to
        the desired sleeping period.
        4. Read the fd associated with the tty line. Since nobody is
        writing any data to that fd, and since we are using blocking I/O,
        the read call will block for the duration specified in the
        vtime field.

Subbu.

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Walter Ro » Sat, 15 Sep 1990 22:18:12


To quote a SunOS man page:
======================================================================
NAME
     usleep - suspend execution for interval in microseconds

SYNOPSIS
     usleep(useconds)
     unsigned useconds;

DESCRIPTION
     Suspend the current process for the number  of  microseconds
     specified  by  the argument.  The actual suspension time may
     be an arbitrary amount longer because of other  activity  in
     the  system,  or because of the time spent in processing the
     call.

     The routine is implemented by setting an interval timer  and
     pausing  until  it occurs.  The previous state of this timer
     is saved and restored.  If the sleep time exceeds  the  time
     to  the expiration of the previous timer, the process sleeps
     only until the signal would have occurred, and the signal is
     sent a short time later.

     This routine is implemented using setitimer()  (see  getiti-
     mer(2));  it  requires  eight  system  calls each time it is
     invoked.  A similar but  less  compatible  function  can  be
     obtained with a single select(2); it would not restart after
     signals, but would not interfere with other uses of  setiti-
     mer.

SEE ALSO
     getitimer(2), sigpause(2V), alarm(3V), sleep(3V), ualarm(3)
======================================================================

wpr
---

System Administrator, Robot Systems Division      UUCP: uunet!cme-durer!rowe
National Institute of Standards and Technology    LIVE: (301) 975-3694

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Sam Fulcom » Wed, 19 Sep 1990 23:01:04



Quote:>I remember reading somewhere that the select() function can be used to

Well, there's an ugly little BSD function called usleep(3) which uses
the itimer stuff.

You can also use select(2) as in:

select(0,0,0,0,&sleepy_time);

(handle signals however you'd like...)

Many Un*xen will have select and perhaps the itimer stuff. Fewer
will have usleep (SYSV jocks have an easier time keeping up with
the BSD system calls...).

Bear in mind that this is all pretty grainy generic stuff. If you
want "real time" control you should look at something marketed
for it.

-s

 
 
 

Can Unix sleep in terms of mili/micro?

Post by Lawrence E. Fre » Sat, 22 Sep 1990 23:15:32


For System V.3 or greater why not use the following code:

/**************************************************************************
 * Function Name         : nap
 * Argument Description  : time to sleep in milliseconds
 * Global References     : (a % by the name indicates an alteration occurs)
 * Author                : Lawrence Freil
 * Function Description  :
 *      This routine uses the poll system call to delay a specified number
 *      of milliseconds.  Poll called without any file descriptors but with
 *      a timeout specified works for this purpose.
 *
 **************************************************************************/
int
nap(int timeout)
{
  struct pollfd foo[1];
  int status;

  status = poll(foo, 0L, timeout);
  return status;

Quote:}

You can also do the same thing for berkley with the select call (no files,
just the timeout.)


        National Science Center Foundation  Phone:(404)-828-8459
        P.O. Box 1648
        Augusta, Ga 30903

--

        National Science Center Foundation  Phone:(404)-828-8459
        P.O. Box 1648
        Augusta, Ga 30903