void execute_command_pipe(char * command_from, char * command_to, char ** args_from, char ** args_to) {
pipe(pipefd);
int pid = fork();
close(pipefd[0]);
if (pid == 0) {
//close(STDOUT_FILENO);
dup2(pipefd[1], STDOUT_FILENO);
int rv1 = execv(get_contain_dir(command_from), args_from);
close(pipefd[1]);
} else {
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
int rv2 = execv(get_contain_dir(command_to), args_to);
close(pipefd[0]);
}
}
例如,如果我想做相当于 ls | grep 测试,父线程将运行 grep 侦听 STDIN 上的输入,子线程将 ls 的输出写入 STDTOUT。