- #include "apue.h"
- static void sig_pipe(int) ; /* our signal handler */
- int
- main(void )
- {
- int n;
- int fd[2];
- pid_t pid;
- char line[MAXLINE];
- if (signal(SIGPIPE,sig_pipe) == SIG_ERR)
- err_sys("signal error");
- if (s_pipe(fd) < 0)
- err_sys("pipe error");
- if ((pid = fork() ) <0 ) {
- err_sys("fork error");
- } else if ( pid > 0) { /* parent child */
- close(fd[1]);
- while(fgets(line,MAXLINE,stdin) != NULL){
- n = strlen(line);
- if (write(fd[0],line,n) != n) {
- err_sys("write line to pipe ");
- }
- if (read(fd[0],line,n) != n) {
- err_sys("read pipe error");
- }
- if (n == 0) {
- err_msg("child closed pipe ");
- break;
- }
- line[n] = 0; /* null terminate */
- if (fputs(line,stdout) == EOF )
- err_sys("fputs error");
- }
- if (ferror(stdin) )
- err_sys("fgets error on stdin ");
- exit(0);
- } else {
- close(fd[0]);
- if ( fd[1] != STDOUT_FILENO &&
- dup2(fd[1],STDOUT_FILENO) != STDOUT_FILENO )
- err_sys("dup2 error on stdout_fileno");
- if ( fd[1] != STDIN_FILENO &&
- dup2(fd[1],STDIN_FILENO) != STDIN_FILENO)
- err_sys("dup2 error on stdin_fileno");
- if (execl("./add","add",(char *) 0) < 0)
- err_sys("execl error");
- }
- exit(0);
- }
- static void
- sig_pipe(int signo){
- printf("sigpipe error for read ");
- exit(1);
- }
- #include "apue.h"
- int s_pipe(int fd[2]) {
- return pipe(fd);
- }
程序的功能和
http://blog.chinaunix.net/uid-22334392-id-3151853.html相同,但是,使用的tream 机制,编译程序后,程序报错,猜测是ubuntu的pipe不支持全双功能所致,正在验证中。。。。。。。。。。
待续!
阅读(1818) | 评论(0) | 转发(0) |