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. ural 1218. Episode N-th: The Jedi Tournament

    1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Je ...

  2. BZOJ2690 : 字符串游戏

    离线算法: 先将所有涉及到的串建成字典树,然后用线段树维护dfs序,时间复杂度$O(m\log L)$. 在线算法: 用替罪羊树动态维护Trie树的dfs序即可,时间复杂度$O(L\log L)$. ...

  3. 【BZOJ】1049: [HAOI2006]数字序列(lis+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题意:给一个长度为n的整数序列.把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希 ...

  4. 【Vijos】1792 摆花

    题目链接:https://vijos.org/p/1792 算法:DP 看到这题真的一点不会...只能爆搜一下..但太太慢了..看了题解后,听说是分组背包??不知道.. 好吧,,还是百度了下题解,渐渐 ...

  5. QT快捷键

    F1使用方法:选中某一类或函数,按下F1,出现帮助文档 F2使用方法:选中某一类或函数,按下F2,迅速定位到该类或函数声明的地方或被调用的地方 Ctrl+鼠标滚轮的使用方法:按住Ctrl,使鼠标滚轮旋 ...

  6. 将MyApp.exe和Autorun.lnk添加到NK里,在project.bib文件内加入

    1. 将应用程序和应用程序快捷方式添加到映像里,再将快捷方式添加到StartUp目录下,这样当系统运行后应用程序就能自动运行:2. 直接替换Wince的SHELL,即修改注册表: [HKEY_LOCA ...

  7. java 遍历文件夹里的文件

    Java遍历文件夹的2种方法: A.不使用递归: import java.io.File; import java.util.LinkedList; public class FileSystem { ...

  8. 【iM_TFTRGB液晶模块】demo例程(版本1.02)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  9. Scrum会议9(Beta版本)

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  10. [ZZ] 景深效果(Depth of Field) , Pass1 将场景渲染到一个RenderTarget,做为清晰版, Pass2: BluredRT , Pass3: WDepth = Depth / Far_Z_Clip

    http://blog.csdn.net/xoyojank/article/details/1883520   什么是景深效果? 景深效果,简称DOF,在人眼跟光学摄像设备上很常见.如下图: 简单地来 ...