管道
来源:互联网 发布:武汉办公软件培训班 编辑:程序博客网 时间:2024/06/08 17:38
什么是管道?
管道是Unix中最古老的进程间通信的形式
我们把从一个进程连接到另一个进程的一个数据流称为一个“管道”
管道限制:
1、管道式半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道
2、只能用于具有共同祖先的进程(具有亲缘关系的进程)之间进行通信;通常,一个管道有一个进程创建,然后该进程调用fork,此后父子进程之间就可以应用该通道。
匿名管道pipe
包含头文件<unistd.h>
功能:创建一无名管道
原型是:
包含头文件<unistd.h>
功能:创建一无名管道
原型
int pipe(int fd[2]);
参数
fd:文件描述符数组,其中fd[0]表示读端, fd[1]表示写端
返回值:成功返回0,失败返回错误代码
事例一:从子进程发送信息到父进程
/* * PipeDemo1.cpp * * Created on: Aug 1, 2014 * Author: zfs */#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <dirent.h>#include <signal.h>/* #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE)) */#define ERROR_EXIT(m) \ do\ { \ perror(m);\ exit(EXIT_FAILURE);\ }\ while(0)int main(int argc, char* argv[]) {int pipefd[2];int ret = pipe(pipefd);if (ret == -1)ERROR_EXIT("pipe error");pid_t pid;pid = fork();if (pid == -1) {ERROR_EXIT("fork error");}if (pid == 0) {close(pipefd[0]); ::write(pipefd[1],"hello",5); close(pipefd[1]); exit(EXIT_SUCCESS);} close(pipefd[1]); char buf[10]={0}; ::read(pipefd[0],buf,5); printf("buf:%s\n",buf);return 0;}输出:hello
事例二:实现 类似ls | wc -w的功能
/* * pipeDemo2.cpp * * Created on: Aug 1, 2014 * Author: zfs */#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <dirent.h>#include <signal.h>/* #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE)) */#define ERROR_EXIT(m) \ do\ { \ perror(m);\ exit(EXIT_FAILURE);\ }\ while(0)int main(int argc, char* argv[]) {int pipefd[2];int ret = pipe(pipefd);if (ret == -1)ERROR_EXIT("pipe error");pid_t pid;pid = fork();if (pid == -1) {ERROR_EXIT("fork error");}if (pid == 0) { //子进程dup2(pipefd[1], STDOUT_FILENO); //文件描述副复制的用法close(pipefd[1]);close(pipefd[0]);::execlp("ls", "ls", NULL);fprintf(stderr, "error execute ls \n");exit(EXIT_FAILURE);}//dup2(pipefd[0], STDIN_FILENO);close(pipefd[0]);close(pipefd[1]);::execlp("wc", "wc", "-w", NULL);fprintf(stderr, "error execute ls \n");exit(EXIT_FAILURE);return 0;}事例三:实现文件copy的作用
/* * pipeDemo2.cpp * * Created on: Jul 31, 2014 * Author: zfs */#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <dirent.h>#include <signal.h>/* #define ERROR_EXIT(m)(perror(m),exit(EXIT_FAILURE)) */#define ERROR_EXIT(m) \ do\ { \ perror(m);\ exit(EXIT_FAILURE);\ }\ while(0)int main(int argc, char* argv[]) {close(0);open("01.cpp", O_RDONLY);close(1);open("10.cpp", O_WRONLY | O_CREAT | O_TRUNC, 0644);execlp("cat", "cat", NULL);return 0;}
0 0
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- 管道
- <>管道
- 管道
- 管道
- 管道
- 管道
- 管道
- |管道
- 【线性扫描 while ijk】总结
- 12款最佳Linux命令行终端工具
- [IDE工具配置]myeclipse 2014 专业版 安装 svn插件
- PAT 1050 String Subtraction (20)
- 关于计算一个字符串中的,数字,字母,空格,其他字符个数说说明
- 管道
- CURL模拟进行微信接口的GET与POST
- Codeforces 155 C. Hometask
- poj图论解题报告索引
- ubuntu10.04 bind9 配置主从区域数据传送问题
- xcode 快捷方式
- oc初级,小小总结
- [杂谈]张宏杰:嘉庆“滑落曲线”
- [LeetCode] Single Number