title: c / interrupted system call / fork vs. thread
body: <p>I discovered an issue with thread implementation, that is strange to me. Maybe some of you can explain it to me, would be great.</p>
  
  <p>I am working on something like a proxy, a program (running on different machines) that receives packets over eth0 and sends it through ath0 (wireless) to another machine which is doing the exactly same thing. Actually I am not at all sure what is causing my problem, that's because I am new to everything, linux and c programming.</p>
  
  <p>I start two threads, </p>
  
  <ul>
  <li>one is listening (socket) on eth0 for incoming packets and sends it out through ath0 (also socket) </li>
  <li>and the other thread is listening on ath0 and sends through eth0.</li>
  </ul>
  
  <p>If I use threads, I get an error like that:</p>
  
  <pre><code>sh-2.05b# ./socketex 
  Failed to send network header packet.
  : Interrupted system call
  </code></pre>
  
  <p>If I use fork(), the program works as expected.
  Can someone explain that behaviour to me?</p>
  
  <p>Just to show the sender implementation here comes its code snippet:</p>
  
  <pre><code>while(keep_going) {
      memset(&amp;buffer[0], '\0', sizeof(buffer));
  
      recvlen = recvfrom(sockfd_in, buffer, BUFLEN, 0, (struct sockaddr *) &amp;incoming, &amp;ilen);
      if(recvlen &lt; 0) {
          perror("something went wrong / incoming\n");
          exit(-1);
      }
  
      strcpy(msg, buffer);
      buflen = strlen(msg);
  
      sentlen = ath_sendto(sfd, &amp;btpinfo, &amp;addrnwh, &amp;nwh,  buflen, msg, &amp;selpv2, &amp;depv);
  
      if(sentlen == E_ERR) {
          perror("Failed to send network header packet.\n");
          exit(-1);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>: my main file, starting either threads or processes (fork)</p>
  
  <pre><code>int main(void) {
  
  port_config pConfig;
  
  memset(&amp;pConfig, 0, sizeof(pConfig));
  pConfig.inPort = 2002;
  pConfig.outPort = 2003;
  
  pid_t retval = fork();
  
  if(retval == 0) {
      // child process
      pc2wsuThread((void *) &amp;pConfig);
  } else if (retval &lt; 0) {
      perror("fork not successful\n");
  } else {
      // parent process
      wsu2pcThread((void *) &amp;pConfig);
  }
  
  /*
  wint8 rc1, rc2 = 0;
  
  pthread_t pc2wsu;
  pthread_t wsu2pc;
  
  rc1 = pthread_create(&amp;pc2wsu, NULL, pc2wsuThread, (void *) &amp;pConfig);
  rc2 = pthread_create(&amp;wsu2pc, NULL, wsu2pcThread, (void *) &amp;pConfig);
  
  if(rc1) {
      printf("error: pthread_create() is %d\n", rc1);
      return(-1);
  }
  
  if(rc2) {
      printf("error: pthread_create() is %d\n", rc2);
      return(-1);
  }
  
  pthread_join(pc2wsu, NULL);
  pthread_join(wsu2pc, NULL);
  */
  return 0;
  }
  </code></pre>
  
  <p>Does it help?</p>
  
  <p><strong>update</strong> 05/30/2011</p>
  
  <pre><code>-sh-2.05b# ./wsuproxy 192.168.1.100
  mgmtsrvc
  mgmtsrvc
  Failed to send network header packet.
  : Interrupted system call
  13.254158,75.165482,DATAAAAAAmgmtsrvc
  mgmtsrvc
  mgmtsrvc
  </code></pre>
  
  <p>Still get the interrupted system call, as you can see above.
  I blocked all signals as followed:</p>
  
  <pre><code>sigset_t signal_mask;
  sigfillset(&amp;signal_mask);
  sigprocmask(SIG_BLOCK, &amp;signal_mask, NULL);
  </code></pre>
  
  <p>The two threads are working on the same interfaces, but on different ports. The problem seems to appear still in the same place (please find it in the first code snippet). I can't go further and have not enough knowledge of how to solve that problem. Maybe some of you can help me here again.</p>
  
  <p>Thanks in advance.</p>