title: execv on a child process is keeping its parents process id in the new program instead of zero
body: <p><strong>Program Description:</strong></p>
  
  <p>I am working on a program to help teach me about processes in C and Linux. The program uses <code>fork</code> to create a child process and then the parent and the child both print out a message a certain amount of times saying <code>Parent Process</code>, <code>Child Process</code> accordingly. To keep things from being scrambled the parent waits for the child process to print all it's messages. To make things even more interesting the child process uses <code>execv</code> to call another program that prints it's messages.</p>
  
  <p>From what I've read on <code>execv</code> a new process is not started; the new process image overlays the original process image. So when the child process starts the new program the process id inside that new program should be the same as the childs process id which is zero.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>When I print out the child's process id before calling <code>execv</code> it is zero (as expected) but as soon as I get into the new program I print out the process id and it is the parents process id (but it should be the childs id).</p>
  
  <p>Could anyone please explain why the new program does not have zero as the process id?</p>
  
  <p><strong>Arguments:</strong></p>
  
  <ul>
  <li><code>Argv[1]</code> = Number of times the message prints for the child process</li>
  <li><code>Argv[2]</code> = Number of times the message prints for the parent process</li>
  <li><code>Argv[3]</code> = Number of seconds waited after a message for the child process</li>
  <li><code>Argv[4]</code> = Number of seconds waited after a message for the parent process</li>
  <li><code>Argv[5]</code> = Message sent to the new program the child process starts (child processes message to be printed).</li>
  </ul>
  
  <p><strong>Initial Program:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int arg, char *argv[]) {
  
  if(arg &lt; 6) {
      printf("\n\t(Task4.c) Please provide the right arguments\n\n");
      exit(EXIT_FAILURE);
  }
  
  char *argv2[3];
  
  argv2[0] = argv[1];
  argv2[1] = argv[3];
  argv2[2] = argv[5];
  
  int Nc = atoi(argv[1]);
  int Np = atoi(argv[2]);
  int Tc = atoi(argv[3]);
  int Tp = atoi(argv[4]);
  int n, exit_code;
  
  pid_t pid;
  char *message;
  
  printf("\n\n(Task4.c)\tfork program starting\n");
  
  pid = fork();
  
  //Let child go first before the parent
  if(pid != 0) {
      int stat_val;
      pid_t child_pid;
  
      child_pid = wait(&amp;stat_val);
  
      printf("\n(Task4.c)\tChild has finished: PID = %d\n", child_pid);
      if(WIFEXITED(stat_val))
          printf("(Task4.c)\tChild exited with code %d\n\n", WEXITSTATUS(stat_val));
      else
          printf("(Task4.c)\tChld terminated abnormally\n");
  }
  
  //Set up the values for the parent and child
  switch(pid)
  {
      case -1:
          perror("(Task4.c)\tfork failed");
          exit(1);
      case 0:
          /* a child execute different program */
          printf("\n(Task4.c)\tpid=%d\n\n", pid);
          if (execv ("child", argv2) &lt;0)
          {
              printf ("(Task4.c)\texecl ERROR");
              exit (EXIT_FAILURE);
          }
          break;
      default:
          message = "This is the parent";
          n = Np;
          exit_code = 0;
          break;
  }
  
  //Print the message for the process using their values
  for(; n &gt; 0; n--) {
      printf("(Task4.c)\tpid=%d  ", pid);
      puts(message);
      switch(pid)
      {
          case 0:
              sleep(Tc);
              break;
          default:
              sleep(Tp);
              break;
      }
  }
  
  printf("\n\n");
  
  exit(exit_code);
  }
  </code></pre>
  
  <p><strong>New Program Being Called By Child Process:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int arg2, char *argv2[]) {
  
  if(arg2 &lt; 3) {
      printf("\n\t(child.c) Please provide the right arguments\n\n");
      exit(EXIT_FAILURE);
  }
  
  int Nc2 = atoi(argv2[0]);
  int Tc2 = atoi(argv2[1]);
  
  int Np2 = 5;
  int Tp2 = 1;
  int n2, exit_code2;
  
  //Here I was expected to get 0 as the pid...
  pid_t pid2 = getpid();
  char *message2;
  
  printf("(child.c)\tchild program starting\n\n");
  printf("(child.c)\tpid=%d  \n\n", pid2);
  
  switch(pid2)
  {
      case -1:
          perror("(child.c) fork failed");
          exit(1);
      case 0:
          message2 = argv2[2];
          n2 = Nc2;
          exit_code2 = 37;
          break;
      default:
          message2 = "(child.c)\tThis is the parent";
          n2 = Np2;
          exit_code2 = 0;
          break;
  }
  
  for(; n2 &gt; 0; n2--) {
      printf("(child.c)\tpid=%d  ", pid2);
      puts(message2);
      switch(pid2)
      {
          case 0:
              sleep(Tc2);
              break;
          default:
              sleep(Tp2);
              break;
      }
  }
  
  exit(37);
  }
  </code></pre>
  
  <p>Example Output:</p>
  
  <ul>
  <li>With arguments as (5 3 1 1 "Hello From Child")</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/925O6.png" alt="enter image description here"></p>