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. commands - `for`

    internal variable of separator: IFS

  2. Android 使用网络ADB调试.

    前提: android Phone和PC在同一局域网内. android Phone 有虚拟终端(CM系统集成了ADB网络调试,比较赞.); 1.在android phone虚拟终端输入 stop a ...

  3. android 图片合成

    package com.ebensz.eink.demo; import java.io.File; import java.io.FileOutputStream; import android.a ...

  4. sublime工具 插件自动补全方法

    自动补全(emmet),输入对应的关键字(html标签)---tab键 http://www.emmet.io/ 代码片段 只需要输入自己的关键字--tab键 操作: 添加代码片段,然后保存 保存 使 ...

  5. Vue.js 学习示例

    本篇和大家分享的是学习Vuejs的总结和调用webapi的一个小示例:快到年底了争取和大家多分享点东西,希望能对各位有所帮助:本章内容希望大家喜欢,也希望各位多多扫码支持和推荐谢谢: » Vuejs ...

  6. 【ecos学习1】wmware运行redboot[方法一]--脚本实现配置

    背景: 远程服务器Ubuntu生成软盘镜像,通过Mac下wmware运行. 1- 环境及版本: uname -a 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 ...

  7. Enormous Input Test Solved Problem code: INTEST

    import sys import psyco #一键优化库 psyco.full() def main(): n, k = map(int, sys.stdin.readline().strip() ...

  8. CodeForces 187A Permutations

    反向思维,先求数组中不用处理的元素个数,再用n减去这个数,得到结果. #include <iostream> #include <cstring> #define maxn 2 ...

  9. 03-树1. List Leaves (25)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  10. ADO.NET帮助类DBHelper

    一. DBHelper帮助类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...