title: execlp() is not properly working after fork()
body: <p>I'm writing function that takes in the value in a linked list, then forks the process and execs a new process using the parameters passed into the command line. Here is the code I have for prog2b.cc:</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "cplist.h"
  #include &lt;sys/time.h&gt;
  #include &lt;iostream&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;errno.h&gt;
  
  struct timeval start, end;
  char *key;
  
  int launch_children(cplist *head){
      pid_t cpid;
      double execution_times = 0; 
      if(cpid = fork()  &lt; 0 ){   // Important to trap errors
              std::cerr &lt;&lt; "ARGH I'm likely in trouble, get me out of here!" &lt;&lt; std::endl;
              exit(1);  // Important to exit on errors
          }else if (cpid == 0 ){
              std::cout &lt;&lt; "Child process "&lt;&lt; getpid() &lt;&lt; ". Running grep on " &lt;&lt; key &lt;&lt; head-&gt;path &lt;&lt; std::endl;
              execlp("prog2b", "prog2b", "grep", "-R", key, head-&gt;path, NULL); 
              std::cerr &lt;&lt; "Exec failed.. Why?" &lt;&lt; std::endl;
              exit(1); // VERY IMPORTANT - DON'T LET CHILD KEEP RUNNING
      } else { // parent
              head-&gt;cpid = cpid;
              wait(NULL); 
              std::cout &lt;&lt; "Child "&lt;&lt; cpid &lt;&lt; "has terminated in " &lt;&lt; execution_times;
      }
  }
  
  
  
  int main(int argc, char *argv[]){
      int i;
      int j = 0;
      cplist *head = (cplist*) malloc(sizeof(cplist));
      head-&gt;path = NULL;
      head-&gt;next = NULL;
  
      if(strcmp(argv[1], "-v") == 0){
          key = argv[2];
          for(i = 3; i &lt; argc; i++){
              cpl_add(head, argv[i]);
              j++;
          }
      } else {
          key = argv[1];
          for(i = 2; i &lt; argc; i++){
              cpl_add(head, argv[i]);
              j++;
          }
      }
  
      printf("key: %s\n", key);
      launch_children(head);
  
  
  return(0);
  }
  </code></pre>
  
  <p>My program is supposed to take in the key and path values from the command line,  then the child process should execlp using 'grep' '-r', and the values passed in. I'm struggling to get the exec to work properly. I've spent a lot of time on the man page for execs trying to understand them better and test out other execs but I've gotten stuck. The exec process won't run. Here is an example to show how it works right now: </p>
  
  <p>When I run: <code>./prog2b 5678 /hw1/02/</code> from the command line my output is: </p>
  
  <pre class="lang-none prettyprint-override"><code>key: 5678
  Child process 70788. Running grep on 5678 /hw1/02
  Exec failed.. Why?
  key: 5678
  Child process 70789. Running grep on 5678 /hw1/02
  Exec failed.. Why?
  </code></pre>
  
  <p>The correct out put should be:</p>
  
  <pre class="lang-none prettyprint-override"><code>key: 5678
  Child process nnnnn. Running grep -R 5678 hw1/02
  
  ../../hw1/02/nYyLimCI:5678
  ../../hw1/02/ANFGmj97:5678
  ../../hw1/02/oFrtX8Sy:5678
  ../../hw1/02/UrYt9aBz:5678
  ../../hw1/02/wE1AMVeh:5678
  ../../hw1/02/F6TGJEiJ:5678
  ../../hw1/02/v1HG6zmh:5678
  ../../hw1/02/YyOSKcJG:5678
  
  
  Child process nnnnn has terminated in a.bbbbbb seconds
  </code></pre>
  
  <p>I know the exec is failing and I've tried to use errno and it outputted "No such file or directory". I've figured out it referred to the first prog2b but when changed to ./prog2b I believe it causes a fork bomb. I also haven't quite grasped how to grep in the exec and I feel that could be the issue. Hopefully this will help clear up my troubles with fork and exec. I have a header file and linked list function class to _add and _dump but I don't believe those are what's causing the bug</p>