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. 举例

#include <unistd.h>
#include <stdio.h> int main( void )
{
int filedes[2];
char buf[80];
pid_t pid; pipe( filedes );
pid=fork();
if (pid > 0)
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else if(pid == 0)
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
} waitpid( pid, NULL, 0 ); return 0;
}

执行结果:

[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);

最新文章

  1. jQuery DateTimePicker 日期和时间插件
  2. Servlet 3.0 异步模式
  3. 21. javacript高级程序设计-Ajax与Comet
  4. sprinvMVC路径拦截
  5. 【CSU1812】三角形和矩形 【半平面交】
  6. @HTML.checkboxFor()用法
  7. 取出type=&quot;button&quot; 和type=&quot;text&quot; 里面的值显示在页面
  8. 漫游Kafka实现篇之分布式
  9. windows批处理命令之ren
  10. Linux程序设计综合训练之简易Web服务器
  11. 深度剖析malloc、free和new、delete
  12. VMware虚拟机在仅主机模式下的网卡无法动态获取IP
  13. H5新增特性、方法
  14. 加密webconfig中的连接字符串,利用RSA非对称加密,利用windows保存密钥容器
  15. OSI的七层模型介绍
  16. 【Java】【7】枚举类
  17. mysql 存储过程简单实例
  18. Java 知识点(转)
  19. 我也说说Emacs吧(1) - Emacs和Vi我们都学
  20. Oracle自定义函数&amp;加密

热门文章

  1. I/O操作技术
  2. Java基础07 包
  3. 解决Charles Response 中文乱码
  4. Perl 面向对象编程的两种实现和比较:
  5. JS将秒换成时分秒
  6. Web用户控件
  7. C# 验证识别基类
  8. Redis 与 Memcache
  9. Enterprise Solution 企业管理软件开发框架
  10. 《Linux命令行与shell脚本编程大全》 第十八章 学习笔记