Timeout module for general use, signal/alarm

Timeout module for general use, signal/alarm

Post by KIM Hiongu » Thu, 31 Dec 1998 04:00:00

Hi experts,

I'm designing a timeout module for general use and here goes my plan.
I will provide following functions or macros for the users of the
A typical use of my timeout module would be:

   if (! timeout_begin(3)) {
       timeout_setjmp(); // it should be a macro call sigsetjmp(),
                         // in order not to cause stack-destruction
                         // at siglongjmp()
       if (! timeout_fired()) {
       } else {
           // I/O operation timed-out
           // error handler here

I intentionally placed _setjmp() in between _begin() and _fired()
to make it surely escape from the I/O operation.

My problem is in the function timeout_begin(). In order to allow
nested timeout calls, I had to know whether there's already some
timeout routine is running or not, either through my timeout
module or through direct access to signal() and alarm(). In case
there's already an alarm handler running before I call
timeout_begin(), the module should backup the previous handler
and the later recover it at timeout_end().

A subtle problem occurs here. On recovering the previous handler,
how many seconds do we have left for it? We have to subtract the
time passed during from timeout_begin() to timeout_end().

Here's an example.

  first_timeout set for 10 seconds
  after 2 seconds, second_timeout set for 2 seconds
  after 2 seconds more, second_timeout fires, first_timeout recovered
  after 6 seconds more, first_timeout fires.

Am I right?

Question: What if I set the second_timeout to be 8 seconds long?
    On recovering the first timeout, only 0 seconds left for the
    first timeout. But alarm(0) disables alarming...

A slightly different situation:
    I know that alarm() returns the seconds remaining from the previous
    alarm(). If alarm() returns 0, what does it mean? Don't we have
    a previously running alarm OR there WAS actually a previously
    running alarm but it just finished, at the same time on the new
    call of alarm(), with the previous signal handler not yet called.

A simple remedy would be a single function that shows BOTH whether
there's a previously running alarm AND how many seconds does it have.

Thanks in advance.

--KIM Hiongun, a warrior against language barrier.


1. Q: equivalent of alarm() using another signal

Hi there,

Like the subject line says, i would need some function that
would do exactly what alarm() does, only using another

Here's the context, if you're interested : I'm working on a
modified version of wuftp that interfaces with a sybase DB.
The problem i'm faced with is that the updates performed on
the database when the ftp session terminates are done
through a function written in Embedded SQL, on which i have
not much control. Anyway, on reception of a SIGALRM, my
connection to the database is closed, and when i get into
the SIGALRM handler in the ftp code and try to update...
well i can't.

Has anyone encountered a similar problem, and do you know if
there's a classic way of implementing timeouts using another
signal ?


G -

2. How to read from a .csv file (with read)?

3. Socket timeout using alarm.

4. Problems with NFS

5. using alarm() and signal

6. TiMidity vs AWE64

7. Using more than one alarm signal?

8. Sendmail & Multiple Domains??

9. Which signal is used to end session by TIMEOUT expired?

10. Problem with sleep and signal/alarm.

11. alarm, signal and read

12. multiple signal alarms possible?

13. signal() and sigaction() and alarm() help please