title: How to stop execve from exiting original program
body: <p>I have written my own shell and wrote a function that handles triple piping but I have an issue with my shell exiting after execve. I believe the issue is that I need to fork an extra time? but I am not entirely sure where, since this executes the piped programs perfectly. Also there is no use of wait(2) in this implementation, not sure if that has something to do with it too. Thanks</p>
  
  <pre><code>int fd[2];
  int fd2[2];
  
  if (pipe(fd) == -1)
  {
      perror("ERROR CREATING PIPE");
      return;
  }
  
  pid_t pid = fork();
  
  if (args4[0] != NULL)
  {
      switch(pid)
      {
          case -1:
              printf("%s\n", "fail to fork");
              return;
          case 0:
              if (pipe(fd2) == -1)
              {
                  perror("ERROR CREATING PIPE");
                  return;
              }
              switch(pid = fork())
              {
                  case -1:
                      printf("%s\n", "fail to fork");
                      return;
                      break;
                  case 0:
                      if ( dup2(fd2[1], STDOUT_FILENO) == -1 )
                      {
                          printf("%s\n", "fail to dup");
                          return;
                      }
                      close(fd2[0]);
                      close(fd2[1]);
                      execve(args2[0], args2, environ);
                      exit(1);
                  default:
                      if ( dup2(fd2[0], STDIN_FILENO) == -1 )
                      {
                          printf("%s\n", "fail to dup");
                          return;
                      }
                      if ( dup2(fd[1], STDOUT_FILENO) == -1 )
                      {
                          printf("%s\n", "fail to dup");
                          return;
                      }
                      close(fd2[0]);
                      close(fd2[1]);
                      execve(args3[0], args3, environ);
                      exit(2);
              }
              exit(3);
  
          default:
              if ( dup2(fd[0], STDIN_FILENO) == -1)
              {
                  printf("%s\n", "fail to dup");
                  return;
              }
              close(fd[0]);
              close(fd[1]);
              printf("%s\n", "4");
              execve(args4[0], args4, environ);
              exit(4);
      }
  }
  </code></pre>