title: Placing a process in the background in C
body: <p>I'm currently diving into creating a backgrounding a job in <code>C</code> with <code>&amp;</code>.  I need to implement a non-blocking <code>waitpid</code> in order for this to work.  I know that.  Also, I already am catching the condition if <code>&amp;</code> is entered at the end of the command line.  I'm just not sure how to exactly send the process with the end to be a background job and also implement it as executing while another prompt is prompting for the next command.<br>
  Anything would help at all, thank you.</p>
  
  <pre><code>    struct bgprocess{
          int pid;
          struct bgprocess * next;
          struct bgprocess * prev;    
      };
  
      struct bgprocess * bgprocess1;
      bgprocess1 = malloc(sizeof(struct bgprocess));
      bgprocess1-&gt;prev = NULL;
      bgprocess1-&gt;next = NULL;
      bgprocess1-&gt;pid = NULL;
  
      struct bgprocess * current;
      current = bgprocess1;
  
      do{
          int bgreturn = 0;
          while (current != NULL){
              if (waitpid(current-&gt;pid, &amp;bgreturn, WNOHANG)){
                  printf("Child exited");
                  current-&gt;prev-&gt;next = current-&gt;next;
                  current-&gt;next-&gt;prev = current-&gt;prev;
                  current-&gt;prev = NULL;
                  current-&gt;next = NULL;
                  free(current);                  
              }
          current = current-&gt;next;    
          }
          if (end){
              int pid = fork();
              if (pid &lt; 0){
                  exit(1);            
              }       
              if (pid) {
                  execvp(args[0], args);          
                  exit(0);            
              }
  
              struct bgprocess * newNode;
              newNode = malloc(sizeof(struct bgprocess));
              newNode-&gt;pid = pid;
              newNode-&gt;next = NULL;
  
              if (current-&gt;next == NULL){
                  current-&gt;next = newNode;        
              }
              while (1){
                  if (current-&gt;next == NULL){
                      current-&gt;next = newNode;
                  }           
                  current = current-&gt;next;
              }
  
          }
      }
      while (current != NULL);
  
      int bgreturn = 0;
      while (current != NULL){
          if (waitpid(current-&gt;pid, &amp;bgreturn,0)){
              printf("Child exited");
              current-&gt;prev-&gt;next = current-&gt;next;
              current-&gt;next-&gt;prev = current-&gt;prev;
              current-&gt;prev = NULL;
              current-&gt;next = NULL;
              free(current);  
          }
          current = current-&gt;next;
      }
      }
  </code></pre>
  
  <p>Alright, so I've been working on this some more and I think I may be starting to understand.  I still have a few syntax errors that I'm unaware of how to fix so I'll probably use gdb or something unless someone else can point them out.  Am I going about it the right way or am I completely wrong?</p>