#include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <pthread.h> void sys_err(const char *str) { perror(str); exit(1); } int main(int argc,char *argv[]) { int fd[2]; int ret; ret = pipe(fd); pid_t pid; if(ret == -1) { sys_err("pipe error");}
//创建子进程 pid = fork(); if(pid == -1) { sys_err("fork error");
}else if(pid > 0) //父进程 { close(fd[1]);
//输入重定向到管道 dup2(fd[0],STDIN_FILENO); execlp("wc","wc","-l",NULL); sys_err("exclp error"); }else if(pid == 0) //子进程 { close(fd[0]);
//输出重定向到管道 dup2(fd[1],STDOUT_FILENO); execlp("ls","ls",NULL); sys_err("exclp error"); } return 0; }
标签:wc,err,dup2,int,pid,C语言,sys,fd,include From: https://www.cnblogs.com/powercool/p/16595523.html