title: How to properly wait for foreground/background processes in my own shell in C?
body: <p>In <a href="https://stackoverflow.com/questions/873620/how-do-i-clear-this-array-pointer-in-c">this</a> previous question I posted most of my own shell code. My next step is to implement foreground and background process execution and properly wait for them to terminate so they don't stay as "zombies".</p>
  
  <p>Before adding the possibility to run them in the background, all processes were running in the foreground. And for that, I simply called wait(NULL) after executing any process with execvp(). Now, I check for the '&amp;' character as the last argument and if it's there, run the process in the background by not calling wait(NULL) and the process can run happily in the background will I'm returned to my shell.</p>
  
  <p>This is all working properly (I think), the problem now, is that I also need to call wait() (or waitpid() ?) somehow so that the background process doesn't remain "zombie". That's my problem, I'm not sure how to do that...</p>
  
  <p>I believe I have to handle SIGCHLD and do something there, but I have yet to fully understand when the SIGCHLD signal is sent because I tried to also add wait(NULL) to childSignalHandler() but it didn't work because as soon as I executed a process in the background, the childSignalHandler() function was called and consequently, the wait(NULL), meaning I couldn't do anything with my shell until the "background" process finished. Which wasn't running on the background anymore because of the wait in the signal handler.</p>
  
  <p>What am I missing in all this?</p>
  
  <p>One last thing, part of this exercise I also need to print the changes of the processes status, like process termination. So, any insight on that is also really appreciated.</p>
  
  <p>This is my full code at the moment:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;wait.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;sys/types.h&gt;
  
  #include "data.h" // Boolean typedef and true/false macros
  
  
  void childSignalHandler(int signum) {
      //
  }
  
  int main(int argc, char **argv) {
      char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
      bool background;
      ssize_t rBytes;
      int aCount;
      pid_t pid;
  
      //signal(SIGINT, SIG_IGN);
  
      signal(SIGCHLD, childSignalHandler);
  
      while(1) {
          write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
          rBytes = read(0, bBuffer, BUFSIZ-1);
  
          if(rBytes == -1) {
              perror("read");
              exit(1);
          }
  
          bBuffer[rBytes-1] = '\0';
  
          if(!strcasecmp(bBuffer, "exit")) {
              exit(0);
          }
  
          sPtr = bBuffer;
          aCount = 0;
  
          do {
              aPtr = strsep(&amp;sPtr, " ");
              pArgs[aCount++] = aPtr;
          } while(aPtr);
  
          background = FALSE;
  
          if(!strcmp(pArgs[aCount-2], "&amp;")) {
              pArgs[aCount-2] = NULL;
              background = TRUE;
          }
  
          if(strlen(pArgs[0]) &gt; 1) {
              pid = fork();
  
              if(pid == -1) {
                  perror("fork");
                  exit(1);
              }
  
              if(pid == 0) {
                  execvp(pArgs[0], pArgs);
                  exit(0);
              }
  
              if(!background) {
                  wait(NULL);
              }
          }
      }
  
      return 0;
  }
  </code></pre>