Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18169
  • 博文数量: 11
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-23 13:22
文章分类

全部博文(11)

文章存档

2011年(1)

2010年(5)

2009年(5)

我的朋友

分类: LINUX

2010-01-03 22:50:00

/* 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);
}




阅读(246) | 评论(0) | 转发(0) |
0

上一篇:pipe01

下一篇:Thinkpad SL410 安装ubuntu 9.10

给主人留下些什么吧!~~