title: Multithreaded program and fork(): alternative or safe implementation
body: <p>In a multithreaded Linux/C++-program, I want to use <code>fork()</code> with a signal handler for <code>SIGCHLD</code>.</p>
  
  <p>In the child process I use <code>open()</code> to create two new file descriptors, <code>sendfile()</code> and <code>close()</code>, then the child exits.</p>
  
  <p>I planned to use <code>fork()</code> to implement the following requirements:</p>
  
  <p>The threads in the parent process shall be able to</p>
  
  <ol>
  <li>detect the normal termination of the child process, and in that case shall be able to create another <code>fork()</code> doing the <code>open()/sendfile()/close()</code> for a range of files</li>
  <li>kill the <code>sendfile()</code>-child process in case of a specific event and detect the intentional termination to clean up</li>
  </ol>
  
  <p>For requirement 1 I could just wait for the result of <code>sendfile()</code>.
  Requirement 2 is why I think I need to use <code>fork()</code> in the first place.</p>
  
  <p>After reading the following posts</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12868724/threads-and-fork-how-can-i-deal-with-that">Threads and fork(). How can I deal with that?</a></li>
  <li><a href="https://stackoverflow.com/questions/1235516/fork-in-multi-threaded-program">fork in multi-threaded program</a></li>
  </ul>
  
  <p>I think that my solution might not be a good one.</p>
  
  <p>My questions are:</p>
  
  <ul>
  <li>Is there any other solution to implement requirement 2 ?</li>
  <li>Or how can I make sure that the library calls <code>open(), close() and sendfile()</code> will be okay?</li>
  </ul>
  
  <p><em>Update:</em></p>
  
  <ul>
  <li>The program will run on a Busybox Linux / ARM</li>
  <li>I've assumed that I should use <code>sendfile()</code> for having the most efficient file transfer due to several posts I've read regarding this topic.
  A safe way to implement my requirement could be using <code>fork()</code> and <code>exec*()</code> with <code>cp</code>, with the disadvantage that the file transfer might be less efficient</li>
  </ul>
  
  <p><em>Update 2:</em></p>
  
  <ul>
  <li>it's sufficient to <code>fork()</code> once in case of a specific event (instead of once per file) since I switched to <code>exec*()</code> with <code>rsync</code> in the child process. However the program needs invoke that <code>rsync</code> always in case of a specific event.</li>
  </ul>