当前位置:网站首页>Unix System Programming Chapter 15 15.2 Pipes
Unix System Programming Chapter 15 15.2 Pipes
2022-08-09 10:28:00 【Mary soda Patty】
Pipe is the oldest form of IPC in UNIX systems, and all UNIX systems provide this communication mechanism.
Pipelines have two limitations:
- Historically, they were half-duplex (ie data only flowed in one direction).Some systems now offer full-duplex pipes.
- Pipes can only be used between two processes with a common ancestor. Usually, a pipe is created by a process. After the process calls fork, the pipe can be used between the parent process and the child process.
We will see that FIFOs do not have the second limitation, and UNIX domain sockets do not have either.
A pipe is created by calling the pipe function.
#include int pipe(int fd[2]);//Return value, return 0 for success, -1 for error
Two file descriptors are returned via the parameter fd: fd[0] is open for reading and fd[1] is open for writing.The output of fd[1] is the input of fd[0].
A pipe for a single process is of little use, usually a process will call pipe followed by fork to create an IPC channel from parent to child and vice versa.
What to do after the fork depends on the direction of data flow we want. For pipes from parent to child, the parent closes the read end of the pipe and the child closes the write end.
The following two rules apply when one end of the pipe is closed.
- When reading a pipe whose write end has been closed, after all data has been read, read returns 0, indicating end of file.
- If writing to a channel whose read end has been closed, the signal SIGPIPE is generated. If the signal is ignored or caught and returned from its handler, write returns -1 and errno is set to EPIPE.
Sample program:
#include "apue.h"int main(){int n;int fd[2];pid_t pid;char line[MAXLINE];if(pipe(fd)<0)err_sys("pipe error");if((pid=fork())<0)err_sys("fork error");else if(pid>0){close(fd[0]);write(fd[1],"hello world\n",12);}else{close(fd[1]);n=read(fd[0],line,MAXLINE);write(STDOUT_FILENO,line,n);}exit(0);}
边栏推荐
- OpenGL ES2.0编程三部曲(转载自MyArrow)
- ESIM(Enhanced Sequential Inference Model)- 模型详解
- By asking where the variables are stored, the shepherd boy laughed and said to use pointers, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the use of go lang type pointers (Poin
- The common problems in laptops, continuously updated
- shell脚本实战(第2版)/人民邮电出版社 脚本1 在PATH中查找程序
- 1005 继续(3n+1)猜想 (25 分)
- 条件控制语句
- Unix Environment Programming Chapter 15 15.9 Shared Storage
- Qt 国际化翻译
- 函数组件和类组件和dva视图更新问题
猜你喜欢
史上最小白之《Word2vec》详解
使用.NET简单实现一个Redis的高性能克隆版(四、五)
深度学习--循环神经网络(Recurrent Neural Network)
循环嵌套以及列表的基本操作
使用cpolar远程连接群晖NAS(创建临时链接)
Battery modeling, analysis and optimization (Matlab code implementation)
Redis + NodeJS 实现一个能处理海量数据的异步任务队列系统
学长告诉我,大厂MySQL都是通过SSH连接的
ESIM(Enhanced Sequential Inference Model)- 模型详解
[相机配置] 海康相机丢包配置环境
随机推荐
markdown转ipynb--利用包notedown
浅析JWT安全问题
今天做了手机播放器的均衡器
2022强网杯WP
hover内部指定子类的样式
单元测试2之实际结果检查的引用
1005 继续(3n+1)猜想 (25 分)
ESIM(Enhanced Sequential Inference Model)- 模型详解
Received your first five-figure salary
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
想了解API接口,这一篇就够了
WUSTOJ:n个素数构成等差数列
实现下拉加载更多
机器学习--朴素贝叶斯(Naive Bayes)
【原创】JPA中@PrePersist和@PreUpdate的用法
Electron application development best practices
MySQL执行过程及执行顺序
2021-01-11-雪碧图做表情管理器
相伴成长,彼此成就 用友U9 cloud做好制造业数智化升级的同路人
unix环境编程 第十五章 15.6 XSI IPC