linux管道编程 100求解..

来源:百度知道 编辑:UC知道 时间:2024/06/02 07:56:39
创建管道 派生一个子进程 执行cat程序 父进程接受用户从终端输入的任何内容 通过管道传递给子进程的cat程序
麻烦给详细点...我基本完全不懂啊 谢谢

//具体不懂得地方你晚上再问我吧
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>

int main(int argc, char *argv) {
int fd[2];
int len;
pid_t pid;
char filename[10];
char childbuf[10];
if (pipe(fd) < 0) {
perror("pipe error!");
exit(1);
}
if ((pid = fork()) < 0) {
perror("fork error!");
exit(1);
}
if (pid == 0) {

close(fd[1]);
len = read(fd[0], childbuf, 100);
childbuf[len] = '\0';
printf("%s\n", childbuf);
if (execlp("cat", "cat", childbuf, (char*) 0) < 0) {
perror("exec error!");
exit(1);
}
} else {