23,121
社区成员
发帖
与我相关
我的任务
分享
#include <unistd.h>
int fd[2];
void run_ls()
{
close(fd[0]);
close(fd[1]);
execve("/bin/ls", NULL, NULL);
}
void run_wc()
{
close(fd[0]);
close(fd[1]);
execve("/usr/bin/wc", NULL, NULL);
}
int main()
{
pipe(fd);
if (fork() > 0) {
dup2(fd[1], 1);
run_ls();
}
else {
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
pipe(fd);
if (fork() > 0) {
dup2(fd[1], 1);
run_wc();
}
else {
dup2(fd[0], 0);
run_wc();
}
}
return 0;
}