title: Multiple timers in C
body: <p>I have an application which needs to do the following:</p>
  
  <ul>
  <li>If an event happens (a disconnect from server), a long timer is started (say 5 minutes). The application then tries to reconnect to the server.</li>
  <li>If the reconnect fails, a short timer is started (20 seconds) which should attempt to reconnect again.</li>
  <li>If it succeeds, the long timer should <strong>keep</strong> going.</li>
  <li>When the long timer expires, if there is no connection, the application should exit, otherwise it should continue as normal.</li>
  </ul>
  
  <p>I am limited in that I cannot use threads, only processes. I also cannot afford to wait for the result of reconnect() to return.</p>
  
  <p>So far I have a design like this:</p>
  
  <pre><code>int main(int argc, char **argv)
  {
      /* do main loop work, if disconnected, call reconnect() &amp; continue doing work */
  }
  
  void reconnect()
  {
      pid = fork();
  
      if (pid &gt;= 0) {
          /*Successful fork*/
          if (pid == 0) {
  
              rv = attempt_reconnect;               
              if (rv == 0) {
                  /*Notify sig_child Success*/
                  exit(0);
              } else {
                  /*Notify sig_child Fail*/
                  exit(1);
              }
          }
      } 
  }
  
  void sig_child(int signum)
  {
      if(fork returned success) {
          set flag to continue network stuff
      }
      else {
          alarm(20);
      }
  }
  
  void sig_alarm(int signo)
  {
      /*Received alarm, trying to reconnect...*/
      reconnect();
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I think I have a solution working from an example <a href="http://www.graphics-muse.org/wp/?p=868" rel="nofollow">here</a>. It allows me to create timers with separate ID's, and then identify which one has has signalled the program</p>