title: c alternative to signal() + alarm()
body: <p>I'm building some FastCGI apps and it sort of bugs me that lighttpd doesn't kill them off after they've been idle, so I'm trying to have them close on their own.</p>
  
  <p>I tried using</p>
  
  <pre><code>signal(SIGALRM, close);
  alarm(300);
  </code></pre>
  
  <p>and having the close function execute exit(0), and that works almost well.</p>
  
  <p>The problem is the close function is being called every time the main program loop runs though (I call alarm(300) each loop to reset it). I've read the man page for alarm() and it doesn't seem as though calling it multiple times with the same value should trip SIGALRM so I'm assuming Lighttpd is sending an alarm signal.</p>
  
  <p>The big question! Is there a way to run a method after a specific interval, and have that interval be resettable without SIGALRM? I'd be nice if I could have multiple alarms as well.</p>
  
  <p>Here's the whole app thus far:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  #include &lt;signal.h&gt;
  #include "fcgiapp.h"
  
  FCGX_Stream     *in, *out, *err;
  FCGX_ParamArray envp;
  int calls = 0;
  
  void print(char*, ...);
  void close();
  
  int main(void)
  {
          // If I'm not used for five minutes, leave
          signal(SIGALRM, close);
  
          int reqCount = 0;
  
          while (FCGX_Accept(&amp;in, &amp;out, &amp;err, &amp;envp) &gt;= 0)
          {
                  print("Content-type: text/plain\r\n\r\n");
  
                  int i = 0;
                  char **elements = envp;
                  print("Environment:\n");
                  while (elements[i])
                          print("\t%s\n", elements[i++]);
  
                  print("\n\nDone. Have served %d requests", ++reqCount);
                  print("\nFor some reason, close was called %d times", calls);
  
                  alarm(300);
          }
  
          return 0;
  }
  
  void print(char *strFormat, ...)
  {
          va_list args;
          va_start(args, strFormat);
          FCGX_VFPrintF(out, strFormat, args);
          va_end(args);
  }
  
  void close()
  {
          calls++;
  //      exit(0);
  }
  </code></pre>