免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 857 | 回复: 0
打印 上一主题 下一主题

pipe02 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-03 22:50 |只看该作者 |倒序浏览

                /* pipe02.c */
#include unistd.h>
#include stdio.h>
#include stdlib.h>
#include fcntl.h>
#include limits.h>
int  main (void)
{
    int   fd[2];
    int   fdin;
    char  read_pipe_buf[PIPE_BUF];
    char  read_file_buf[PIPE_BUF];
    pid_t pid;
    int   read_pipe_len;
    int   read_file_len;
    /* creat a pipe */
    if ((pipe(fd))  0){
        perror("pipe error");
        exit(1);
    }
    pid = fork();
    if (pid  0){
        perror("fork error");
        exit(1);
    }
    /* child */
    if (0 == pid){
        close(fd[1]);            /* close wite pipe */
        while((read_pipe_len = read(fd[0], read_pipe_buf, PIPE_BUF)) > 0){
            read_pipe_buf[read_pipe_len] = '\0';
            printf("read %d bytes\n", read_pipe_len);
            printf("%s\n", read_pipe_buf);
        }
        close(fd[0]);
    }
    /* parent */
    if (pid > 0){
        close(fd[0]);
        fdin = open("pipe01.c", O_RDONLY);
        if (fdin  0){
            perror("open error");
            exit(1);
        }
    }
    read_file_len = read(fdin, read_file_buf, PIPE_BUF);
    if (read_file_len > 0){
        write(fd[1], read_file_buf, read_file_len);
    }
    close(fdin);
    close(fd[1]);
    waitpid(pid, NULL, 0);   /* wait for child */
    exit(0);
}

               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103542/showart_2138551.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP