1. 函数说明

pipe(建立管道):
1) 头文件 #include<unistd.h>
2) 定义函数: int pipe(int filedes[2]);
3) 函数说明: pipe()会建立管道,并将文件描述词由参数filedes数组返回。
              filedes[0]为管道里的读取端
              filedes[1]则为管道的写入端。
4) 返回值:  若成功则返回零,否则返回-1,错误原因存于errno中。

错误代码:
         EMFILE 进程已用完文件描述词最大量
         ENFILE 系统已无文件描述词可用。
         EFAULT 参数 filedes 数组地址不合法。

2. 举例

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. int main( void )
  4. {
  5. int filedes[2];
  6. char buf[80];
  7. pid_t pid;
  8. pipe( filedes );
  9. pid=fork();
  10. if (pid > 0)
  11. {
  12. printf( "This is in the father process,here write a string to the pipe.\n" );
  13. char s[] = "Hello world , this is write by pipe.\n";
  14. write( filedes[1], s, sizeof(s) );
  15. close( filedes[0] );
  16. close( filedes[1] );
  17. }
  18. else if(pid == 0)
  19. {
  20. printf( "This is in the child process,here read a string from the pipe.\n" );
  21. read( filedes[0], buf, sizeof(buf) );
  22. printf( "%s\n", buf );
  23. close( filedes[0] );
  24. close( filedes[1] );
  25. }
  26. waitpid( pid, NULL, 0 );
  27. return 0;
  28. }

运行结果:

[root@localhost src]# gcc pipe.c
[root@localhost src]# ./a.out
This is in the child process,here read a string from the pipe.
This is in the father process,here write a string to the pipe.
Hello world , this is write by pipe.

当管道中的数据被读取后,管道为空。一个随后的read()调用将默认的被阻塞,等待某些数据写入。

若需要设置为非阻塞,则可做如下设置:

fcntl(filedes[0], F_SETFL, O_NONBLOCK);
        fcntl(filedes[1], F_SETFL, O_NONBLOCK);

linux pipe的更多相关文章

  1. Linux pipe 源代码分析

    Linux pipe 源代码分析      管道pipe作为Unix中历史最悠久的IPC机制,存在各个版本号的Unix中,主要用于父子进程之间的通信(使用fork,从而子进程会获得父进程的打开文件表) ...

  2. how to using Linux pipe command output another command's help content to a file

    how to using Linux pipe command output another command's help content to a file Linux tee > >& ...

  3. Linux pipe函数

    1. 函数说明 pipe(建立管道): 1) 头文件 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe ...

  4. Linux pipe功能

    1. 功能说明 pipe(管道建设): 1) 头 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe() ...

  5. Linux IPC实践(2) --匿名PIPE

    管道概念 管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个"管道", 管道的本质是固定大小的内核缓冲区; 如:ps aux | gre ...

  6. 记录一次因subprocess PIPE 引起的线上故障

    sence:python中使用subprocess.Popen(cmd, stdout=sys.STDOUT, stderr=sys.STDERR, shell=True) ,stdout, stde ...

  7. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  8. Oracle 【IT实验室】数据库备份与恢复之一:exp/imp(导出与导入&装库与卸库)

    1.1  基本命令 1.  获取帮助 $ exp help=y $ imp help=y     2.  三种工作方式 (1)交互式方式 $ exp        //  然后按提示输入所需要的参数 ...

  9. [ 转载 ] Handler详解

    带着问题学习 Android Handler 消息机制 Marker_Sky 关注  0.4 2018.02.06 18:04* 字数 3992 阅读 541评论 0喜欢 13   学习 Androi ...

随机推荐

  1. 模拟退火算法-[HDU1109]

    模拟退火算法的原理模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到 ...

  2. BZOJ4345 : [POI2016]Korale

    只考虑第一问,将珠子按照价值从小到大排序,设排序后第$i$小的为$b[i]$,定义二元组$(x,y)$表示当前珠子的总价值为$x$,用的价值最大的珠子为$y$,用一个小根堆来维护所有状态.一开始往堆中 ...

  3. 追本溯源 解析“大数据生态环境”发展现状(CSDN)

    程学旗先生是中科院计算所副总工.研究员.博士生导师.网络科学与技术重点实验室主任.本次程学旗带来了中国大数据生态系统的基础问题方面的内容分享.大数据的发展越来越快,但是对于大数据的认知大都还停留在最初 ...

  4. WP7:模拟开始屏幕Tile漂动效果

    在WP7手机的开始屏幕,如果你Hold住某一个瓷贴,就会发现除了你按住的那个瓷贴其他全部下沉半透明,然后开始在不停地漂来漂去~~今天来模仿一下这个效果.新建一个项目,然后在Grid里放一个ListBo ...

  5. 【BZOJ】1834: [ZJOI2010]network 网络扩容(最大流+费用流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1834 我又思考人生了T_T,nd的数组开小了,一直wa,调了一个小时才发现啊!!!!!我一直以为我的 ...

  6. BZOJ4011: [HNOI2015]落忆枫音

    Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题.  「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们 ...

  7. 【C语言】13-指针和字符串

    字符串回顾 一个字符串由一个或多个字符组成,因此我们可以用字符数组来存放字符串,不过在数组的尾部要加上一个空字符'\0'. char s[] = "李洪强"; 上面的代码定义了一个 ...

  8. csv格式

    Name,Password nmae:xiaofan,password:1234567890 每个逗号就是一列

  9. shell中的case语句

    case语法: case $arg in arg1) 语句1 ;; arg2) 语句2 ;; *) help 语句 ;; esac eg: eg:

  10. ArcEngine 不能再打开其他表了

    在IFeatureClass.Search()是弹出这个问题,根据网上的资料,采用 System.Runtime.InteropServices.Marshal.ReleaseComObject()或 ...