[comp.unix.internals] Can Unix sleep in terms of mili/micro?

TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu (09/10/90)

        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.

- Beng Hang (email: taybengh@nusdiscs)

tli@phakt.usc.edu (Tony Li) (09/10/90)

In article <24437@adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
    
            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
Internet: tli@usc.edu Uucp: usc!tli
Thus spake the master programmer: "A well written program is its own
heaven; a poorly-written program its own hell."

2307GILLV%MUCSD.BITNET@cunyvm.cuny.edu (09/10/90)

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

tomw@orac.esd.sgi.com (Tom Weinstein) (09/10/90)

In article <11899@chaph.usc.edu>, tli@phakt.usc.edu (Tony Li) writes:
> In article <24437@adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
>     
>             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);
}

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

--
Tom Weinstein
Silicon Graphics, Inc., Entry Systems Division, Window Systems
tomw@orac.esd.sgi.com
Any opinions expressed above are mine, not sgi's.

boissier@irisa.fr (franck boissiere) (09/10/90)

From article <11899@chaph.usc.edu>, by tli@phakt.usc.edu (Tony Li):
>     
> 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.
--
Franck BOISSIERE                        boissier@irisa.irisa.fr
Prototyping Lab Manager                 boissier@ccettix.UUCP
C.C.E.T.T.   B.P. 59                    boissier%irisa.irisa.fr@uunet.uu.net
35512 CESSON SEVIGNE CEDEX  FRANCE    

nanook@rwing.UUCP (Robert Dinse) (09/11/90)

In article <24437@adm.BRL.MIL>, TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
> 
>         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.
> 
> - Beng Hang (email: taybengh@nusdiscs)

     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.

yukngo@obelix.gaul.csd.uwo.ca (Cheung Yukngo) (09/11/90)

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

james@dlss2.UUCP (James Cummings) (09/11/90)

In article <24437@adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
>
>        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:

main()
{
	sleep_less_than_sec(25);
	exit(0);
}

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.

-- 
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
|Disclaimer:                      | James Cummings                           |
|  You can't blame me!            |   UUCP:                                  |
|  I'm ignorant!                  |    ..swblat!{texbell!texnet..            |
|+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+|              swgate!dlss1..}!dlss2!james |
|Send flames to:                  |   NET:                                   |
|  sowc@devnull.com               |      jc@smunews                          |
|                                 |                                          |
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

mutchler@zule.EBay.Sun (Dan Mutchler) (09/12/90)

In article <84@dlss2.UUCP> james@dlss2.UUCP (James Cummings) writes:

   In article <24437@adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
   >
   >        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:

   main()
   {
	   sleep_less_than_sec(25);
	   exit(0);
   }

   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.

Unless a context switch occurs, which is quite possible. That may
still be okay if times much greater than 25 clock cycles are okay.
That is you need a sleep that sleeps at least x milliseconds.

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.
--

Dan Mutchler                       | ARPA/Internet:  mutchler@EBay.Sun.COM
Sun Federal System Engineer        | UUCP:           ...!sun!mutchler
--------------------------------------------------------------------------
Work hard. Play hard. Eat hard. Sleep hard. Grow big.
Wear glasses, If you need them.
     -The Webb Wilder Credo

morrell@hpcuhb.HP.COM (Michael Morrell) (09/13/90)

/comp.unix.internals/james@dlss2.UUCP (James Cummings)/5:11 am  Sep 11, 1990/
>>        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

cpcahil@virtech.uucp (Conor P. Cahill) (09/13/90)

In article <MUTCHLER.90Sep11140510@zule.EBay.Sun> mutchler@zule.EBay.Sun (Dan Mutchler) writes:
>In article <84@dlss2.UUCP> james@dlss2.UUCP (James Cummings) writes:
>
>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 

sxm@bebop.Philips.Com (Sandeep Mehta) (09/13/90)

  james@dlss2.UUCP (James Cummings) writes:

    TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu writes:
      >
      >        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
restrictions of the process that uses it. I know that some folks @
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
--
sxm@philabs.Philips.Com                  	"Jazz is Happiness" - Le Sony'Ra

johnv@metaware.metaware.com (John Vinopal) (09/14/90)

I seem to recall mutchler@zule.EBay.Sun (Dan Mutchler) saying:
 >   james@dlss2.UUCP (James Cummings) writes:
 >   In article <24437@adm.BRL.MIL> TAYBENGH%NUSDISCS.BITNET writes:
 >   >        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.
 >
 >   main()
 >   {
 >	   sleep_less_than_sec(25);
 >	   exit(0);
 >   }
 >
 >   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.
 
You assume that you have a remarkably dumb compiler.  How is that code any
different to an optimizing compiler than say:

	#define DEBUG 0
	foobar(int x)
	{
		int i;

		for (i = 0; i <= x; i++)
			if ( DEBUG ) {
				crash_n_burn();
			}
	}

You wouldn't want THAT code stuck in your executable now would you?
Both come out to be NULL and both should be removed!  Optimize, optimize,
optimize!

-- 
The Wailer at the Gates of Dawn		     | johnv@metaware.com           |
#include <vomit.h>                           | uunet!metaware!johnv         |
void puke(struct dinner *p) { free(p); }     | banshee@ucsc{b,f}.UCSC.EDU   |
2,3,5,7,13,17,19,31,61,89,107,127,521,607....| banshee@ucsc{b,f}.BITNET     |

vs_subbu@apollo.HP.COM (V. S. Subrahmanyam) (09/14/90)

In article <37960001@hpcuhb.HP.COM> morrell@hpcuhb.HP.COM (Michael Morrell) writes:
>/comp.unix.internals/james@dlss2.UUCP (James Cummings)/5:11 am  Sep 11, 1990/
>>>        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.

rowe@cme.nist.gov (Walter Rowe) (09/14/90)

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
---
Walter P. Rowe                                    ARPA: rowe@cme.nist.gov
System Administrator, Robot Systems Division      UUCP: uunet!cme-durer!rowe
National Institute of Standards and Technology    LIVE: (301) 975-3694

sgf@cs.brown.edu (Sam Fulcomer) (09/18/90)

In article <YUKNGO.90Sep11095526@obelix.gaul.csd.uwo.ca> yukngo@obelix.gaul.csd.uwo.ca (Cheung Yukngo) writes:
>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

lef@dogwood.atl.ga.us (Lawrence E. Freil) (09/21/90)

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;
}

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

	Lawrence Freil	       		    Usenet/DDN:lef@dogwood.atl.ga.us
        National Science Center Foundation  Phone:(404)-828-8459
	P.O. Box 1648
	Augusta, Ga 30903

-- 
	Lawrence Freil	       		    Usenet/DDN:lef@dogwood.atl.ga.us
        National Science Center Foundation  Phone:(404)-828-8459
	P.O. Box 1648
	Augusta, Ga 30903