linux pipe
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);
linux pipe的更多相关文章
- Linux pipe 源代码分析
Linux pipe 源代码分析 管道pipe作为Unix中历史最悠久的IPC机制,存在各个版本号的Unix中,主要用于父子进程之间的通信(使用fork,从而子进程会获得父进程的打开文件表) ...
- 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 > >& ...
- Linux pipe函数
1. 函数说明 pipe(建立管道): 1) 头文件 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe ...
- Linux pipe功能
1. 功能说明 pipe(管道建设): 1) 头 #include<unistd.h> 2) 定义函数: int pipe(int filedes[2]); 3) 函数说明: pipe() ...
- Linux IPC实践(2) --匿名PIPE
管道概念 管道是Unix中最古老的进程间通信的形式,我们把从一个进程连接到另一个进程的一个数据流称为一个"管道", 管道的本质是固定大小的内核缓冲区; 如:ps aux | gre ...
- 记录一次因subprocess PIPE 引起的线上故障
sence:python中使用subprocess.Popen(cmd, stdout=sys.STDOUT, stderr=sys.STDERR, shell=True) ,stdout, stde ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- Oracle 【IT实验室】数据库备份与恢复之一:exp/imp(导出与导入&装库与卸库)
1.1 基本命令 1. 获取帮助 $ exp help=y $ imp help=y 2. 三种工作方式 (1)交互式方式 $ exp // 然后按提示输入所需要的参数 ...
- [ 转载 ] Handler详解
带着问题学习 Android Handler 消息机制 Marker_Sky 关注 0.4 2018.02.06 18:04* 字数 3992 阅读 541评论 0喜欢 13 学习 Androi ...
随机推荐
- cocos2d ccitemimage
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class H ...
- BZOJ3641 : 货车运输
若一条边的v小于等于u,则贡献为l*w/v,否则贡献为l*w/u 将边按v从小到大排序,将询问按u从小到大排序 用树链剖分维护链上和,val[0]表示第一种情况下的贡献,val[1]表示第二种情况下的 ...
- BZOJ3073 : [Pa2011]Journeys
用线段树套链表维护所有边,用set维护未访问过的点 然后BFS,每次在线段树上找边,然后在set中查询点 一条边使用之后就没有用了,所以在链表中将它删去 时间复杂度$O((n+m)\log n+m\l ...
- android sdk 安装排错
如果你遇到了消息为“Failed to fetch URL…” 的错误提示,那么你需要将HTTPS方式改为HTTP方式,方法如下: 碰到这样错误,请按下边的操作. 1)在菜单选择Tools—Optio ...
- 常用正则表达式(?i)忽略字母的大小写!
1.^/d+$ //匹配非负整数(正整数 + 0) 2.^[0-9]*[1-9][0-9]*$ //匹配正整数 3.^((-/d+)|(0+))$ //匹配非正整数(负整数 + 0) 4.^-[0-9 ...
- HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示
我们知道,当用户发送一个http请求的时候,浏览的的版本信息也包含在了http请求信息中: 如上图所示,请求 google plus 请求头就包含了用户的浏览器信息: User-Agent:Mozil ...
- 封装同步的UIActionSheet
封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...
- iOS移动开发周报-第25期
iOS移动开发周报-第25期 [摘要]:本期iOS移动开发周报带来如下内容:苹果发布 iPhone6 和 Apple Watch.Swift 1.0 GM发布.Xcode 6支持PDF Vector作 ...
- PreparedStatement与Statement的区别
PreparedStatement与statement的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象 ...
- GIT: 远程建立一个仓库,然后复制到本地
1. 登录 GIT,创建一个新的仓库 gitskills 2. 创建的时候,要选择 Initialize this repository with a readme ,让GitHub初始化仓库 3. ...