BACON@MTUS5.BITNET (Jeffery Bacon) (12/15/89)
Given sleep(arg); unsigned arg. Nothing new. But what if you want to sleep for less than one second, say, 0.5? (Why isn't terribly important here.) (Please email replies; I don't read comp.lang.c much, and I'm behind in my reading anyway...thanx.) ------- Jeffery Bacon -- Computing Technology Svcs., Michigan Technological University email- bacon@mtus5.bitnet voice: (906)487-2110 fax: (906)487-2787 alternate- uucp: <world>!itivax!anet!bacos domain: bacos%anet@itivax.iti.org
peter@sersun1.essex.ac.uk (Allott P) (12/19/89)
In article <89348.231211BACON@MTUS5.BITNET> BACON@MTUS5.BITNET (Jeffery Bacon) writes: >Given sleep(arg); unsigned arg. Nothing new. >But what if you want to sleep for less than one second, say, 0.5? It is possible to "sleep" for less than one second by doing a selcect(.......) with an appropriate value in the timeval (5th param I think) and with no channels to check (2nd through 4th params I think). See the documentation for full details.
davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (12/20/89)
In article <2754@servax0.essex.ac.uk> peter@essex.ac.uk (Allott P) writes: | It is possible to "sleep" for less than one second by doing a | selcect(.......) with an appropriate value in the timeval (5th param I think) | and with no channels to check (2nd through 4th params I think). | See the documentation for full details. Correct. There are also vendor dependent routines in other versions. Common names are mspeel (in ms), usleep (in us, but clock tick resolution), and nap (ms again). nap is xenix and 5.3.2 and later, uspeep is SunOS and some others, don't know where msleep is from, got some source with msleep(50); /* wait 50 ms */ in it, so I know it's out there somewhere. nap and select are your best bets for portability. I *think* there's a version of nap for BSD, but I don't have it here. -- bill davidsen (davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen) "The world is filled with fools. They blindly follow their so-called 'reason' in the face of the church and common sense. Any fool can see that the world is flat!" - anon
tneff@bfmny0.UU.NET (Tom Neff) (12/20/89)
In article <1937@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes: >[how to sleep less than a second?] >Common names are mspeel (in ms), usleep (in us, but clock tick >resolution), and nap (ms again). > > nap is xenix and 5.3.2 and later ... However the argument to nap() in Sys V/386 is in HUNDREDTHS of a second, not in milliseconds. V/386 CURSES provides a separate napms() function for millisecond precision. Go figure. -- "We plan absentee ownership. I'll stick to `o' Tom Neff building ships." -- George Steinbrenner, 1973 o"o tneff@bfmny0.UU.NET
marc@tekig3.PEN.TEK.COM (Marc Frajola) (12/20/89)
In article <2754@servax0.essex.ac.uk> peter@essex.ac.uk (Allott P) writes: >In article <89348.231211BACON@MTUS5.BITNET> BACON@MTUS5.BITNET (Jeffery Bacon) writes: >>Given sleep(arg); unsigned arg. Nothing new. >>But what if you want to sleep for less than one second, say, 0.5? > >It is possible to "sleep" for less than one second by doing a >selcect(.......) with an appropriate value in the timeval (5th param I think) >and with no channels to check (2nd through 4th params I think). >See the documentation for full details. Hi... Some time ago, I was posed with the problem of doing the equivalent of nap() on BSD. Since SysV had nap(), I decided to implement nap() with BSD primitives. I've used it in one of my programs for the last two years without problems (so it is SOMEWHAT tested). I distribute it in nap.c and only compile it on BSD systems; SysV already has nap() somewhere in a library. Following is my version of nap() -- please e-mail me if you make useful enhancements/changes... Bon Appetit! ...Marc... -- Marc Frajola, Tektronix Inc., Beaverton, OR Phone: (503) 627-4340 (Tek) or (503) 643-5203 (Home) InterNet-Style Address: marc@tekig3.PEN.TEK.COM UUCP: ..!tektronix.TEK.COM!tekig3.PEN.TEK.COM!marc (Tek - Lab Scopes) ..!tektronix.TEK.COM!tessi.UUCP!escargot!marc (Home System e-mail) ---------- Cut here for 'nap.shar' ------------------------------------- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # nap.c # This archive created: Tue Dec 19 15:39:41 1989 export PATH; PATH=/bin:$PATH echo shar: extracting "'nap.c'" '(2496 characters)' if test -f 'nap.c' then echo shar: will not over-write existing file "'nap.c'" else sed 's/^X//' << \SHAR_EOF > 'nap.c' X/* X * Implementation of System-V style nap() on BSD Unix (4.2 and after) X * X * Copyright (C) 1987, 1988, 1989 by Marc A. Frajola X * X * Permission to use, copy, modify, and distribute this software and X * its documentation for any purpose and without fee is hereby X * granted, provided that the above copyright notice appear in all X * copies and that both that copyright notice and this permission X * notice appear in supporting documentation. This software is X * provided "as is" without express or implied warranty. X * X * The nap() function under SysV may be implemented quite differently, X * but the idea here is to delay a specified number of milliseconds X * since sleep()'s granularity is only every second. X * X * This code is provided here is for BSD, and will function ONLY on a X * 4.2bsd or later UNIX system. X * X * WARNING: This function will blow away any previous alarm setting if X * the alarm() function uses setitimer(). X * X * By Marc A. Frajola, 08/20/87 X * X * $Header: nap.c,v 1.6 89/12/19 14:21:33 marc Exp $ X */ X X#include <sys/time.h> X#include <signal.h> X Xstatic int napwakeup(); /* Forward reference for wakeup function */ Xstatic int napdone; /* Flag if nap() call is done */ X X/* X * Function for emulating System-V like nap() behavior using BSD X * setitimer() X */ Xnap(msec) X int msec; /* Number of milliseconds to nap */ X{ X struct itimerval value; /* Timer value structure for setitimer() */ X long usec; /* Number of microseconds before alarm */ X int (*oldsig)(); /* Old signal condition */ X X /* Set the timer reload values for no followup SIGALRMs: */ X value.it_interval.tv_sec = 0; X value.it_interval.tv_usec = 0; X X /* Set the current timer for the desired number of milliseconds: */ X usec = msec * 1000; X value.it_value.tv_sec = msec / 1000; X value.it_value.tv_usec = usec % 1000000; X X /* X * Set the timer, done flags, and wakeup procedure: X */ X napdone = 0; X oldsig = signal(SIGALRM, napwakeup); X if (setitimer(ITIMER_REAL, &value, (struct itimerval *)0) < 0) { X perror("setitimer"); X return; X } X X /* Block the process until a SIGALRM comes in: */ X do { X pause(); X } while (napdone != 1); X X /* Reset signal() state back to original condition: */ X signal(SIGALRM, oldsig); X X return; X} X X/* X * This function only gets executed so that pause() will unblock the X * process. X */ Xstatic Xnapwakeup() X{ X /* Set flag so nap() knows the SIGALRM has actually come in: */ X napdone = 1; X X return; X} SHAR_EOF if test 2496 -ne "`wc -c < 'nap.c'`" then echo shar: error transmitting "'nap.c'" '(should have been 2496 characters)' fi fi # end of overwriting check # End of shell archive exit 0
wozniak@utkux1.utk.edu (Bryon Lape) (12/21/89)
I wrote a sleep() function for Quick C that will go less than one second, but I am not sure how far down, -bryon-
prc@erbe.se (Robert Claeson) (12/21/89)
In article <2754@servax0.essex.ac.uk>, peter@sersun1.essex.ac.uk (Allott P) writes: > In article <89348.231211BACON@MTUS5.BITNET> BACON@MTUS5.BITNET (Jeffery Bacon) writes: > >Given sleep(arg); unsigned arg. Nothing new. > >But what if you want to sleep for less than one second, say, 0.5? > It is possible to "sleep" for less than one second by doing a > selcect(.......) with an appropriate value in the timeval (5th param I think) > and with no channels to check (2nd through 4th params I think). The same trick can be done under UNIX System V Release 3.x using the poll() system call with appropriate values and no stream fd's to check. -- Robert Claeson E-mail: rclaeson@erbe.se ERBE DATA AB
guy@auspex.auspex.com (Guy Harris) (12/21/89)
> uspeep is SunOS and some others,
"usleep" is 4.3BSD and anybody who picked it up from there (which
includes SunOS 3.2 and later, as I remember).
ofl@kos.rci.dk (Ole Frank Larsen) (05/08/90)
I, who normally writes UNIX programs, was writing a small MS-DOS program yesterday. When I tried to compile it with MSC 5.1 there was an unresolved external viz.: _sleep(). Looking in the manual I utterly failed to find any sleep(), wait(), delay() or whatever this function may be called. Is it possible that sleep() has not been implemented in the standard libraries? I could of course write one myself, but I would like to know whether anyone has observed this before. Another short question: How do you empty the keyboard buffer without having to read all the keystrokes? I have tried fflush(stdin), but this does not work. Thank you in advance -- Ole Frank Larsen. RC International, Denmark ofl@rci.dk Hackers do it with fewer instructions