Writing my own shell… stuck on pipes?

前端 未结 9 712
攒了一身酷
攒了一身酷 2020-12-30 03:41

For the past few days I have been attempting to write my own shell implementation but I seem to have gotten stuck on getting pipes to work properly. I am able to parse a li

9条回答
  •  长情又很酷
    2020-12-30 04:11

    well, I don't have an answer, but I am working on the same problem atm. I will share what i have. It works for the two commands, but after it is done running, i/o are broken. in a strange way i haven't been able to figure out yet. call the plumber!

    void pipeCommand(char** cmd1, char** cmd2) {
      int fds[2]; // file descriptors
      pipe(fds);
      int oldIn, oldOut;
      // child process #1
      if (fork() == 0) {
        // Reassign stdin to fds[0] end of pipe.
        oldIn = dup(STDIN_FILENO);
        dup2(fds[0], STDIN_FILENO);
        close(fds[1]);
        close(fds[0]);
        // Execute the second command.
        execvp(cmd2[0], cmd2);
      // child process #2
      } else if ((fork()) == 0) {
        oldOut = dup(STDOUT_FILENO);
        // Reassign stdout to fds[1] end of pipe.
        dup2(fds[1], STDOUT_FILENO);
        close(fds[0]);
        close(fds[1]);
        // Execute the first command.
        execvp(cmd1[0], cmd1);
      // parent process
      } else
        wait(NULL);
        dup2(oldIn, STDIN_FILENO);
        dup2(oldOut, STDOUT_FILENO);
        close(oldOut);
        close(oldIn);
    }
    

    I have a feeling it has to do with what am or am not doing after the wait()

提交回复
热议问题