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. 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  2. BZOJ3838 : [Pa2013]Raper

    将选取的$A$看成左括号,$B$看成右括号,那么答案是一个合法的括号序列. 那么只要重复取出$k$对价值最小的左右括号,保证每时每刻都是一个合法的括号序列即可. 将$($看成$1$,$)$看成$-1$ ...

  3. POJ 3691 (AC自动机+状态压缩DP)

    题目链接:  http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...

  4. http://developer.51cto.com/art/200512/15883.htm

    http://developer.51cto.com/art/200512/15883.htm

  5. js html5推送 实例

    <!DOCTYPE html>   <html>   <head>   <title>Simple Webkit notification exampl ...

  6. JAVA计算文件大小

    File f = new File(save_path+File.separator + resouce_id+".zip"); FileInputStream fis = new ...

  7. 【BZOJ】1059: [ZJOI2007]矩阵游戏(二分图匹配)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1059 本题可以看出,无论怎样变化,在同一行和同一列的数永远都不会分手---还是吐槽,,我第一眼yy了 ...

  8. (function(){})()的用法

    最近在整理javascript 学习,发现这个问题了 ,在网上发现这么个解释 最清楚 最明白 : (function(){})() 相当于先定义 function xx(){},后调用 xx(); ( ...

  9. 通过console口连接交换机

    最近发现有人不会通过console口连接交换机. 想想当初我还是小白的时候也是如此啊,如是写下教程. 虽然略简单... 1.连线: console线:(Console---usb) 2.安装驱动 (可 ...

  10. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...