- #include "apue.h"
- int
- main(void)
- {
- int n;
- int fd[2];
- pid_t pid;
- char line[MAXLINE];
- if (pipe(fd) < 0)
- err_sys("pile error");
- if (( pid = fork() ) < 0)
- {
- err_sys("fokr error");
- } else if ( pid > 0) {
- close(fd[0]) ; /* parent close read pile */
- write(fd[1],"hello world",12);
- } else {
- close(fd[1]); /* child read write pile */
- n=read(fd[0],line,MAXLINE);
- write(STDOUT_FILENO,line,n);
- }
- exit(0);
- }
gcc -Wall -o c15-1 c15-1.c error.c
./c15-1
ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ hello world
父进程关闭读端,子进程关闭写端,然后将父进程的输出写入子进程的输入,然后进行打印。
阅读(940) | 评论(0) | 转发(0) |