Pipes, dup2 and exec()
问题 I have to write a shell that can run pipes. For example commands like ls -l | wc -l ". I have successfully parsed the command given by the user as below: "ls" = firstcmd "-l" = frsarg "wc" = scmd "-l" = secarg Now I have to use two forks since the commands are two and a pipe. The code block that I wrote to exec the command is the following: pid_t pid; int fd[2]; pipe(fd); pid = fork(); if(pid==0) { dup2(fd[WRITE_END], STDOUT_FILENO); close(fd[READ_END]); execlp(firstcmd, firstcmd, frsarg,