ttfn,
Mike
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
ttfn,
Mike
Reply to ve...@home.com or post. Thank you.
When you're about to call sleep(), do the following: If there's an alarm
pending, and it's schedule to go off before the sleep would finish, only
sleep until the time the alarm would have gone off. If it's scheduled to
go off after, save away the alarm time; when the sleep finishes, call
alarm() again.
--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
You can use select.
void my_sleep(int sec)
{
struct timeval tim;
tim.tv_sec = sec;
tim.tv_usec = 0;
select(0,NULL,NULL,NULL,&tim);
}
But the my_sleep() blocking will return if the signal happen.
--
=================================================
游順益: 天蠍座
E-mail dan...@info4.csie.nctu.edu.tw
-------------------------------------------------
羔羊無己,獅子無懼
=================================================
If during a sleep() your previously set alarm() times out, then
your handler will be called as you expect but the sleep() will
then return prematurely. Is that the problem?
Remember sleep() returns the number of unslept seconds in this case.
If you want the sleep() to last the full time,
then you can use something like:
void deepsleep(int n)
{
long target=time(NULL)+n; /* you might want n+1 here */
while (time(NULL)<target)
n=sleep(n);
}
In this case your alarm handler could set a "signalled" variable
and check that after the deepsleep()
Also, sleep() is clever enough to restore your alarm call
(in the case of your alarm() lasting longer than the sleep) so
as to take account of the time slept.
so:
alarm(7);
sleep(3);
/* your alarm(7) now has 4 secs to run */
Other things to look at are itimers and select() with a timeout.
hope this is of some use
Rob
I think what he means is that some (old) systems implement `sleep'
something like
void nop(int unused) { }
int sleep(int secs) {
void (*old)(int);
old = signal(SIGALRM, nop);
alarm(secs);
pause();
signal(SIGALRM, old);
}
Obviously that would present a problem if you wanted to use SIGALRM.
Actually, I wonder if the poster's system is such. Probably most modern
Unices don't. (Linux I know doesn't.)
--
Nate Eldredge
na...@cartsys.com