Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754309
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2012-04-04 22:37:45


点击(此处)折叠或打开

  1. #include "apue.h"
  2. static void sig_pipe(int) ; /* our signal handler */
  3. int
  4. main(void )
  5. {
  6.     int n;
  7.     int fd[2];
  8.     pid_t pid;
  9.     char line[MAXLINE];

  10.     if (signal(SIGPIPE,sig_pipe) == SIG_ERR)
  11.         err_sys("signal error");
  12.     if (s_pipe(fd) < 0)
  13.         err_sys("pipe error");
  14.     if ((pid = fork() ) <0 ) {
  15.         err_sys("fork error");
  16.     } else if ( pid > 0) { /* parent child */
  17.         close(fd[1]);
  18.         while(fgets(line,MAXLINE,stdin) != NULL){
  19.             n = strlen(line);
  20.             if (write(fd[0],line,n) != n) {
  21.                 err_sys("write line to pipe ");
  22.             }
  23.             if (read(fd[0],line,n) != n) {
  24.                 err_sys("read pipe error");
  25.             }
  26.             if (n == 0) {
  27.                 err_msg("child closed pipe ");
  28.                 break;
  29.             }
  30.             line[n] = 0; /* null terminate */
  31.             if (fputs(line,stdout) == EOF )
  32.                 err_sys("fputs error");
  33.         }
  34.         if (ferror(stdin) )
  35.             err_sys("fgets error on stdin ");
  36.         exit(0);
  37.     } else {
  38.         close(fd[0]);
  39.         if ( fd[1] != STDOUT_FILENO &&
  40.                 dup2(fd[1],STDOUT_FILENO) != STDOUT_FILENO )
  41.             err_sys("dup2 error on stdout_fileno");
  42.         if ( fd[1] != STDIN_FILENO &&
  43.                 dup2(fd[1],STDIN_FILENO) != STDIN_FILENO)
  44.             err_sys("dup2 error on stdin_fileno");
  45.         if (execl("./add","add",(char *) 0) < 0)
  46.             err_sys("execl error");
  47.     }
  48.     exit(0);
  49. }
  50. static void
  51. sig_pipe(int signo){
  52.     printf("sigpipe error for read ");
  53.     exit(1);
  54. }

点击(此处)折叠或打开

  1. #include "apue.h"
  2. int s_pipe(int fd[2]) {
  3.     return pipe(fd);
  4. }


程序的功能和http://blog.chinaunix.net/uid-22334392-id-3151853.html相同,但是,使用的tream 机制,编译程序后,程序报错,猜测是ubuntu的pipe不支持全双功能所致,正在验证中。。。。。。。。。。
待续!













阅读(1769) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~