Pipe commands with fork and dup2
问题 I wrote the following code in order to pipe two commands: #include <stdlib.h> #include <unistd.h> char *program_1[3] = {"/bin/cat", "/dev/random", NULL}; char *program_2[2] = {"/bin/ls", NULL}; char *program_3[2] = {"/usr/bin/sort", NULL}; int main(void) { int fd[2]; int pid; pipe(fd); if ((pid = fork()) == 0) //Child process { dup2(fd[1], STDOUT_FILENO); close(fd[0]); execve(program_3[0], program_3, NULL); } else if (pid > 0) //Parent process { dup2(fd[0], STDIN_FILENO); close(fd[1]); execve