text: /* Example from Head First C.
  
  Downloaded from https://github.com/twcamper/head-first-c
  
  Modified by Allen Downey.
  */
  
  #include <stdio.h>
  #include <unistd.h>
  #include <string.h>
  #include <stdlib.h>
  #include <errno.h>
  #include <sys/types.h>
  #include <wait.h>
  
  
  void error(char *msg)
  {
      fprintf(stderr, "%s: %s\n", msg, strerror(errno));
      exit(1);
  }
  
  int main(int argc, char *argv[])
  {
      if (argc < 2) {
          fprintf(stderr, "Usage: %s <search phrase>\n", argv[0]);
          return 1;
      }
      const char *PYTHON = "/usr/bin/python2";
      const char *SCRIPT = "rssgossip.py";
      char *feeds[] = {
          "http://www.nytimes.com/services/xml/rss/nyt/Africa.xml",
          "http://www.nytimes.com/services/xml/rss/nyt/Americas.xml",
          "http://www.nytimes.com/services/xml/rss/nyt/MiddleEast.xml",
          "http://www.nytimes.com/services/xml/rss/nyt/Europe.xml",
          "http://www.nytimes.com/services/xml/rss/nyt/AsiaPacific.xml"
      };
      int num_feeds = 5;
      char *search_phrase = argv[1];
      char var[255];
      pid_t pid;
      int status;
      int i;
  
      for (i=0; i<num_feeds; i++) {
  
          printf("Creating child %d.\n", i);
          sprintf(var, "RSS_FEED=%s", feeds[i]);
          char *vars[] = {var, NULL};
          pid = fork();
  
          /* check for an error */
          if (pid == -1) {
              fprintf(stderr, "fork failed: %s\n", strerror(errno));
              perror(argv[0]);
              exit(1);
          }
  
          /* see if we're the parent or the child */
          if (pid == 0) {
              //This is the main problem with the original. When the python script ends,
              //so too does this one. We get around this by having it kill off the child processes
              // allowing all articles to be read
              int res = execle(PYTHON, PYTHON, SCRIPT, search_phrase, NULL, vars);
              if (res == -1) {
                  error("Can't run script.");
              }
          }
      }
  
  
      for (i=0; i<num_feeds; i++) {
          pid = wait(&status);
  
          if (pid == -1) {
              fprintf(stderr, "wait failed: %s\n", strerror(errno));
              perror(argv[0]);
              exit(1);
          }
  
          // check the exit status of the child
          status = WEXITSTATUS(status);
          printf("Child %d exited with error code %d.\n", pid, status);
      }
  
  
      return 0;
  }
  