当我使用 ls\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
int unipipe(char* lhs[], char* rhs[])
{
int pfd[2];
int status, cid;
pid_t pid;
char buf;
if((lhs != NULL) && (rhs != NULL))
{
if(pipe(pfd) != 0)
{
perror("pipe");
return -1;
}
if((pid = fork()) < 0)
{
perror("fork");
return -1;
}
else if(pid == 0)
{
close(1); //close the unused read end
dup2(pfd[1], STDOUT_FILENO);
//execute the left-hand side command
close(pfd[0]);
execvp(lhs[0], lhs);
_exit(EXIT_SUCCESS);
}
if(setpgid(pid, 0) < 0)
{
perror("setpgid");
return -1;
};
cid = waitpid(pid, &status, 0);
if((pid = fork()) == 0)
{
close(0);
dup2(pfd[0], STDIN_FILENO);
close(pfd[1]); //close the unused write end
execvp(rhs[0], rhs);
_exit(EXIT_SUCCESS);
}
else
{
waitpid(pid, &status, 0);
}
}