[ale] Mixing Alarm and Sleep

Charles Shapiro cshapiro at nubridges.com
Tue Jan 22 08:49:47 EST 2002


As previously noted, you can't really expect alarm() and sleep() to work
together. But you can use select(2) as a substitute for sleep, as:

void waitForaWhile(long int num_seconds_to_timeout)
{
   struct timeval timeout;
   memset(&timeout,0,sizeof(timeout));
   timeout.tv_sec=num_seconds_to_timeout;

   select(0,NULL,NULL,NULL,&timeout);
   timeout.tv_sec=num_seconds_to_timeout;

}

The 2nd set of timeout.tv_sec is necessary because select will change
the value to 0 when it comes back (the amount of time left before
something popped).

I made this work on a Debian Potato system not a week ago without
trouble.
See the man page on select(2) for details.

-- CHS

-----Original Message-----
From: Chris Fowler [mailto:cfowler at outpostsentinel.com]
To: ale at ale.org
Sent: Monday, January 21, 2002 2:53 PM
To: ale at ale.org
Cc: cfowler at linuxiceberg.com
Subject: [ale] Mixing Alarm and Sleep



Ah!  I'm having some issues using the alarm() call and the sleep() call
in the same program.  The alarm call is in a library function.
Basically, another piece of source code.  It uses the alarm to timeout
on sending
and receiveing data over udp.   I nede to loop and sleep for 30 seconds.
The sleep is not sleeping for 30 seconds.   Bacically it would look like
this.

<<snip>>

---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be
sent to listmaster at ale dot org.






More information about the Ale mailing list