Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!udel!rochester!PT.CS.CMU.EDU!IUS1.CS.CMU.EDU!edw From: edw@IUS1.CS.CMU.EDU (Eddie Wyatt) Newsgroups: comp.lang.c,comp.unix.questions Subject: msleep Message-ID: <332@PT.CS.CMU.EDU> Date: Mon, 9-Nov-87 14:07:44 EST Article-I.D.: PT.332 Posted: Mon Nov 9 14:07:44 1987 Date-Received: Wed, 11-Nov-87 06:52:30 EST Sender: netnews@PT.CS.CMU.EDU Organization: Carnegie-Mellon University, CS/RI Lines: 99 Keywords: sleep millisecs Xref: mnetor comp.lang.c:5346 comp.unix.questions:4853 /************************************************************************** * * * msleep * * * ************************************************************************** Purpose : What you all been waiting for -- A sleep that works in microseconds!!!. Actually it depends on the granularity of the interrupt timer, but at least its better than the second wait. Programmer : Eddie Wyatt (orginally written by Paul Allen) Date : November 1987 Input : sec - number of seconds to sleep msec - number of micro seconds to sleep Output : None Locals : off - itimer value to turn off the timer interrupt. time - wait time oldtime - old itime value bob - the old interrupt handler Globals : ************************************************************************/ #include #include /* ARGSUSED */ static int signal_handler(sig,code,scp) int sig, code; struct sigcontext *scp; { return(1); } static struct itimerval off = {{0,0},{0,0}}; void msleep(sec,msec) int sec, msec; { struct itimerval time; struct itimerval oldtime; int (*bob)(); if ((sec == 0) && (msec == 0)) return; /* note that we have the timer interrupting continously as oppose to just once. This is because if we were to install the itimer to interrupt just once, there would be a critial section (the space between setitimer and sigpause) where if the interrupt sound then, it would be lost and we would wait forever in sigpause */ time.it_interval.tv_sec = sec; time.it_interval.tv_usec = msec; time.it_value.tv_sec = sec; time.it_value.tv_usec = msec; /* insert my interrupt handler and itimer val (order matters) while saving old values to be re-instated */ bob = signal(SIGALRM, signal_handler); if (setitimer(ITIMER_REAL, &time, &oldtime) < 0) perror("setitimer"); /* wait until an interrupt happens, hopefully a timer interrupt - but it need not be. */ sigpause(0); /* turn timer off while returning timer and interrupt handler back to the old state (order matters) */ if (setitimer(ITIMER_REAL, &off, (struct itimerval *) 0) < 0) perror("setitimer"); if ((int) signal(SIGALRM, bob) == -1) perror("signal"); if (setitimer(ITIMER_REAL, &oldtime, (struct itimerval *) 0)) perror("setitimer"); } -- That one looks Jewish -- And that one's a coon -- Who let all this riff raff into the room -- There's one smoking a joint -- and Anoter with spots -- If I had my way -- I'd have all of you shot ... Pink Floyd, "In the Flesh" Eddie Wyatt e-mail: edw@ius1.cs.cmu.edu