博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux pipe功能
阅读量:7051 次
发布时间:2019-06-28

本文共 1399 字,大约阅读时间需要 4 分钟。

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 
#include
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);

 

转载地址:http://agsol.baihongyu.com/

你可能感兴趣的文章
Laravel 添加自定义辅助函数
查看>>
【JVM类加载机制】从一个对象的验证问题说开去
查看>>
django 1.8 官方文档翻译:13-1-2 使用Django认证系统
查看>>
img元素下有空白
查看>>
cacti ERROR 处理
查看>>
saltstack mutilple master 高可用研究
查看>>
Linux环境下 RabbitMQ 的下载与安装
查看>>
mysql无备份恢复
查看>>
Node.js之Buffer
查看>>
AIoT真正向万亿级市场落地的关键是什么?
查看>>
关于k8s集群容器日志收集的总结
查看>>
WordPress 5文章编辑真难用 换回老版经典编辑器教程
查看>>
第二十二章:动画(三)
查看>>
redis-shake数据迁移工具
查看>>
如何保护对外暴露的 Kubernetes 服务
查看>>
2018年人工智能带来了哪些变化,2019年又会发生什么?
查看>>
Python知识点:理解和使用装饰器 @decorator
查看>>
牵扯256万人!国内一AI公司人脸识别数据泄露
查看>>
Linux基础命令---lpc打印机控制
查看>>
Atari 游戏得分提升两个数量级:Uber AI 的新强化学习算法 Go-Explore
查看>>