#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd[2] = {0};
int ret = pipe(fd);
if (ret < 0)
{
perror("pipe faild");
exit(1);
}
pid_t pid = fork();
if (pid < 0)
{
perror("fork faild");
exit(1);
}
else if (pid == 0)
{
close(fd[0]);
int newfd = dup2(fd[1], STDOUT_FILENO);
if (newfd < 0)
{
perror("dup2 faild");
exit(1);
}
execlp("ls", "ls", NULL);
close(fd[1]);
}
else
{
close(fd[1]);
int newfd = dup2(fd[0], STDIN_FILENO);
if (newfd < 0)
{
perror("dup2 faild");
exit(1);
}
execlp("wc", "wc", "-l", NULL);
close(fd[0]);
}
return 0;
}
