title: C: select() - Signal interrupt
body: <p>I'm writing a multithreaded server program in C that works with AF_UNIX sockets.
  The basic structure of the server is:</p>
  
  <ul>
  <li>Main thread initialize data structures and spears a pool of "worker" threads.</li>
  <li>Worker threads start waiting for new requests on an empty thread-safe queue</li>
  <li>Main thread listen on various sockets (new connection and already connected clients) with a <code>select()</code> call.
  
  <ul>
  <li><code>select()</code> reveals possible read on connection socket: main thread calls <code>accept()</code> and puts the returned file descriptor in the <code>fd_set</code> (read set).</li>
  <li><code>select()</code> reveal possible read on already connected sockets: main thread removes the ready file descriptors from the <code>fd_set</code> (read set) and  puts them in the thread-safe queue.</li>
  </ul></li>
  <li>Worker thread extracts a file descriptor from the queue and starts to communicate with the linked client for serve the request. At the end of the service worker thread puts socket file descriptor back to the <code>fd_set</code> (i worte a function to make this operation thread-safe) and it returns waiting again on the queue for a new request.</li>
  </ul>
  
  <p>This routine is repeated in a infinite cycle until a SIGINT is raised.
  Another function has to be performed on SIGUSR1 without exiting from the cycle.</p>
  
  <p>My doubt is about this because if I raise a SIGINT my program exit with <code>EINTR = Interrupted system call</code>.
  I know about the <code>pselect()</code> call and the "self pipe" trick but i can't figure out how to make the things work in a multithreaded situation.</p>
  
  <p>I'm looking for a (POSIX compatible) signal management that that prevent the <code>EINTR</code> error while main thread is waiting on <code>pselect()</code>.</p>
  
  <p>I post some pieces of code for clarification:</p>
  
  <p>Here i set up signal handlers (ignore <code>errorConsolePrint</code> function)</p>
  
  <pre><code>if(signal(SIGINT, &amp;on_SIGINT) == SIG_ERR)
  {
      errorConsolePrint("File: %s; Line: %d; ", "Setting SIGINT handler", __FILE__, __LINE__);
      exit(EXIT_FAILURE);
  }
  if(signal(SIGTERM, &amp;on_SIGINT) == SIG_ERR)
  {
      errorConsolePrint("File: %s; Line: %d; ", "Setting SIGINT handler", __FILE__, __LINE__);
      exit(EXIT_FAILURE);
  }
  if(signal(SIGUSR1, &amp;on_SIGUSR1) == SIG_ERR)
  {
      errorConsolePrint("File: %s; Line: %d; ", "Setting to SIGUSR1 handler", __FILE__, __LINE__);
      exit(EXIT_FAILURE);
  }
  
  if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
  {
      errorConsolePrint("File: %s; Line: %d; ", "Setting to ignore SIGPIPE", __FILE__, __LINE__);
      exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>Here i set up signal mask for <code>pselect</code></p>
  
  <pre><code>sigemptyset(&amp;mask);
  sigemptyset(&amp;saveMask);
  sigaddset(&amp;mask, SIGINT);
  sigaddset(&amp;mask, SIGUSR1);
  sigaddset(&amp;mask, SIGPIPE);
  </code></pre>
  
  <p>Here i call <code>pselect</code></p>
  
  <pre><code>test = saveSet(masterSet, &amp;backUpSet, &amp;saveMaxFd);
  CHECK_MINUS1(test, "Server: creating master set's backup ");
  
  int test = pselect(saveMaxFd+1, &amp;backUpSet, NULL, NULL, &amp;waiting, &amp;mask);
  if(test == -1 &amp;&amp; errno != EINTR)
  {
      ...error handling...
      continue;
  }
  </code></pre>
  
  <p>Hope in some help!
  Thank you all in advance.</p>