title: How to use fork() to daemonize a child process independent of its parent?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client">In Linux, how to prevent a background process from being stopped after closing SSH client</a>  </p>
  </blockquote>
  
  
  
  <p>I have a <strong>C</strong> program which I access and interact with over terminal (usually from SSH on a linux box).  I have been trying to find the solution to the problem where after I close the terminal/logout, the process ends with it (the program basically asks for some options then goes about its business with no further interaction required so I would like to have it continue to run even after I logout of SSH).  </p>
  
  <p>There are ways in linux to avoid this such as 'screen', but I want to do it programatically with <strong>C</strong> without relying on installed packages such as screen- even if this means reinventing the wheel.</p>
  
  <p>So far I understand <code>fork()</code> to be the standard trivial way to daemonize a process, so could anyone help me to finish the code that allows the above described process to happen?</p>
  
  <p>Within parent:</p>
  
  <pre><code> main()
  {
  
  //Do interactive stuff
  
  signal(SIGCHLD, SIG_IGN); //stops the parent waiting for the child process to end
  
  if(fork())
   exit(0);
  // and now the program continues in the child process
  </code></pre>
  
  <p>I can now logout of SSH which closes the original shell...and the child continues its work!</p>
  
  <p>Within child:</p>
  
  <pre><code>//Continue with processing data/whatever the program does (no input/output to terminal required)
  exit(0);
  </code></pre>