Path: utzoo!utgpu!attcan!uunet!husc6!purdue!decwrl!megatest!djones From: djones@megatest.UUCP (Dave Jones) Newsgroups: comp.lang.c Subject: Re: Delay for a fraction of a second in C Message-ID: <965@goofy.megatest.UUCP> Date: 3 Nov 88 22:26:13 GMT References: <2804@ingr.UUCP> Organization: Megatest Corporation, San Jose, Ca Lines: 52 From article <2804@ingr.UUCP>, by crossgl@ingr.UUCP (Gordon Cross): > In article <1145@orion.cf.uci.edu>, echarne@orion.cf.uci.edu (Eli B. Charne) writes: >> >> The Unix sleep command will only go in increments of one second. I was >> wondering if someone new of a nice little routine I could use, or has >> written one that will let me pause (in the Unix operating system) for >> a fraction of a second. If it could go to 100th of a second, that would >> be great! > > You are not going to be able to do this without coding up some kind of > "delay loop" yourself. Of course that solution is definitely NOT portable in > any way since the loop would have to be fine tuned to your machines execution > speed. Remember that in the UNIX kernal, scheduled wakeups occur at fixed > one second intervals. Thus, unless you are using a non standard kernal, sleeps > of only whole numbers of seconds are possible! > > > Gordon Cross > Intergraph Corp. Huntsville, AL Mr. Cross, perhaps it is time that *you* should wake up. You are dreaming up answers. :-) Pour yourself a cup of strong coffee, then try the following: #include delay(seconds, micro_seconds) long seconds; long micro_seconds; { struct timeval timev; timev.tv_sec = seconds; timev.tv_usec = micro_seconds; select(0,0,0,0, &timev); } Some unixes may name the include-file and the structure "struct time_val", but it comes to the same thing. Read the man-pages on select(), setitimer(), and signal(). Also, please don't code tight spin-loops for delays or for polling. Particularly if you are on a time-shared system. Best regards, Dave J.