pipe()函数在子进程产生之前就应该存在。

  • 父子进程之间只进行一次传递

     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:onepipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 512
    int main(int argc, const char *argv[])
    {
    int pipefd[];
    pid_t pid; if(pipe(pipefd) == -){
    perror("pipe failed");
    exit(-);
    }
    printf("%u\n",pid);
    pid == fork();//这里一个个大大的bug,自己的误操作,debug了很久才搞定了
    printf("%u\n",pid);
    if( == pid){
    close(pipefd[]);//0 read 1 write
    //一个约定,父子进程都需遵守 char buf[N];
    memset(buf,,N);
    read(pipefd[],buf,N);
    printf("child read:%s\n",buf); printf("child exit\n");
    exit();
    }else{
    close(pipefd[]);//0 read
    char line[N];
    printf("parent begin\n"); memset(line,,N);
    fgets(line,N,stdin); write(pipefd[],line,strlen(line));
    printf("parent exit\n");
    wait(NULL);//等待子进程的结束
    }
    return ;
    }
  • 父子进程通过管道,进行多次读写操作,先贴上一个比较奇葩的方法(就是一个错误):

     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:my_pipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 1024
    int main(int argc, const char *argv[])
    {
    int fds[];
    if(pipe(fds) == -){//只能有一对进行读写
    perror("failed");
    exit();
    }
    pid_t pid = fork();
    if(pid == -){
    perror("error");
    exit();
    } while(){
    if(pid == ){//child read
    close(fds[]);//1 write
    char buf[] = "";
    read(fds[],buf,);//只能有一个读,
    printf("child read:%s\n",buf);
    //exit(1);
    }else{//parent write close(fds[]);//0 read
    // char *p = "hello,donald";
    char line[N];
    // memset(line,0,N);
    fgets(line,N,stdin);
    write(fds[],line,strlen(line));
    //wait(NULL);
    }
    }
    return ;
    }

    再贴上正确的方法:

     /*============================================
    > Copyright (C) 2014 All rights reserved.
    > FileName:twopipe.c
    > author:donald
    > details:
    ==============================================*/
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 512
    int main(int argc, const char *argv[])
    {
    int pipefd[];
    pid_t pid;
    //pid = fork(); if(pipe(pipefd) == -){
    perror("pipe failed");
    exit(-);
    }
    pid = fork();
    if(pid == ){
    close(pipefd[]);//0 read
    char buf[N];
    while(){
    memset(buf,,N);
    if(read(pipefd[],buf,N) == ){
    break;
    }
    printf("child read:%s\n",buf);
    }
    printf("child exit\n");
    exit();
    }else{
    close(pipefd[]);
    char line[N];
    while(memset(line,,N),fgets(line,N,stdin) != NULL ){
    write(pipefd[],line,strlen(line));
    }
    close(pipefd[]);
    printf("parent exit\n");
    wait(NULL);
    }
    return ;
    }

管道通信之无名管道---pipe()的更多相关文章

  1. Linux 进程通信(无名管道)

    无名管道 无名管道是半双工的,就是对于一个管道来讲,只能读,或者写. 无名管道只能在相关的,有共同祖先的进程间使用(即一般用户父子进程). 一个fork或者execve调用创建的子进程继承了父进程的文 ...

  2. linux进程篇 (三) 进程间的通信1 管道通信

    通信方式分4大类: 管道通信:无名管道 有名管道 信号通信:发送 接收 和 处理 IPC通信:共享内存 消息队列 信号灯 socke 网络通信 用户空间 进程A <----无法通信----> ...

  3. Linux 进程间通信 无名管道(pipe)

    无名管道: 1)只能用于具有亲缘关系的进程之间的通信(无名管道是某一个进程创建的,不像普通文件有路径,在文件系统中是不可见的,其他进程要想打开,只能通过继承的方式去打开) 2)半双工的通信模式,具有固 ...

  4. linux命名管道通信过程

    前一个道,这节学习命名管道. 二命名管道 无名管道仅仅能用来在父子进程或兄弟进程之间进行通信,这就给没有亲缘关系的进程之间数据的交换带来了麻烦.解决问题就是本节要学习的还有一种管道通信:命名管道. 命 ...

  5. PHP多进程编程(2):管道通信

    一个进程如果是个人英雄主义,那么多进程就是集体主义.(不严格区分多进程 和 多线程的差别) 你不再是一个独行侠,而是一个指挥家. 独来独往,非常自由自在,但是,很多时候,不如众人拾柴火焰高. 这就是我 ...

  6. c# c++通信--命名管道通信

    进程间通信有很多种,windows上面比较简单的有管道通信(匿名管道及命名管道) 最近做个本机c#界面与c++服务进行通信的一个需求.简单用命名管道通信.msdn都直接有demo,详见下方参考. c+ ...

  7. linux进程的管道通信

    linux进程的管道通信 要求 编程实现进程的管道通信,掌握管道通信的同步和互斥机制. 相关函数 pipe管道 指用于连接一个读进程和一个写进程以实现他们之间通信的一个共享文件,又名pipe文件.向管 ...

  8. Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

    一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o te ...

  9. 进程间通信IPC之--无名管道(pipe)和有名管道(fifo)(转)

     进程间通信IPC之--无名管道(pipe)和有名管道(fifo) 2012-01-17 22:41:20 分类: C/C++ 每个进程各自有不同的用户地址空间,任何一个进 程的全局变量在另一个进程中 ...

随机推荐

  1. CDZSC_2015寒假新人(1)——基础 f

    Description An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u i ...

  2. oracle存储参数(storage子句)含义及设置技巧

    可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5(数据块) 2(数据块) 操作系统限定 分配给Segment的第一个Extent的大小 ...

  3. 详解如何在linuxmint上用源码包安装nodejs

    第一步:安装依赖包   因为Linuxmint 和 Ubuntu 同属 Debian系的Linux,而且Linuxmint是建立在Ubuntu的基础上的,所以Ubuntu下的命令和包,Linuxmin ...

  4. centos安装phpMyAdmin

     phpMyAdmin是一个网络接口,通过它可以管理你的MySQL数据库. 首先,我们使CentOS系统RPMForge软件库的phpMyAdmin,而不是官方的CentOS 6.2库: 所以需要导入 ...

  5. FPGA的LE数与门数的关系(转)

    一般而言FPGA等效门数的计算方法有两种 一是把FPGA基本单元(如LUT+FF,ESB/BRAM)和实现相同功能的标准门阵列比较,门阵列中包含的门数即为该FPGA基本单元的等效门数,然后乘以基本单元 ...

  6. delphi 获取北京时间(使用XMLHTTP获取百度的时间,WebBrowser获取www.timedate.cn的时间)

    方法一: uses ComObj, DateUtils; function GetInternetTime: string; var XmlHttp: OleVariant; datetxt: str ...

  7. apache httpd, nginx, tomcat, jboss

    web上的server都叫web server,但是大家分工也有不同的. nginx常用做静态内容服务和代理服务器(不是你FQ那个代理),直面外来请求转发给后面的应用服务(tomcat,django什 ...

  8. Handler机制原理图、源码、使用!!!!!

    android的消息处理机制——Looper,Handler,Message  (原理图.源码) 转自:http://my.oschina.net/u/1391648/blog/282892 在开始讨 ...

  9. EasyUI Combotree 只允许选择 叶子节点

    $("#SDID").combotree({ url: '/Ajax/GetDeptTree.aspx?level=4&pid=-1', onSelect: functio ...

  10. HDU 4721 Food and Productivity (二分+树状数组)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :给出n * m的格子,每个格子有两个属性f ...