首页 > 系统服务 > 详细

Linux管道编程实例

时间:2014-10-01 22:00:11      阅读:427      评论:0      收藏:0      [点我收藏+]
#include <unistd.h>
#include <signal.h>
#include <stdio.h>

char parent[] = "a message from parrent";
char child[] = "a message from child";

main ()
{
    int chan1[2], chan2[2];
    int pid;
    char buf[100];
    pipe(chan1);
    pipe(chan2);
    pid=fork();
    if(pid > 0)
    {
        close(chan1[0]);
        close(chan2[1]);
        write(chan1[1], parent, sizeof(parent));
        close(chan1[1]);
        read(chan2[0],buf, 100);
        printf("%s\n", buf);
        close(chan2[0]);
    }
    else if(pid == 0)
    {
        close(chan1[1]);
        close(chan2[0]);
        read(chan1[0], buf, 100);
        printf("%s\n", buf);
        write(chan2[1], child, sizeof(child));
        close(chan2[1]);
        close(chan1[0]);
    }
}




Linux管道编程实例

原文:http://blog.csdn.net/svitter/article/details/39720703

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!