LinuxC/C++编程(4)—管道通信
来源:互联网 发布:苏州海默淘宝开店培训 编辑:程序博客网 时间:2024/06/06 00:16
管道是Linux为进程提供的一种通信方式,这里所说的管道只能用于有血缘关系的进程(一般是子进程和父进程之间)。一般用来做进程同步和进程间通信。
Linux中提供的有名管道的建立函数为:int pipe(int pipe[2]);
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 数组地址不合法。*/
对于管道的读写都是直接通过读写文件描述符filedes完成。且默认阻塞,即管道数据满的时候写操作被阻塞,数据空的时候读操作被阻塞。
下面贴一段简单的代码:
#include <iostream>#include <unistd.h>#include <sys/wait.h>using namespace std;int main(){int pipefd[2];pipe(pipefd); //包含于<unistd.h>,定义了一个管道,同时也打开了参数的文件描述符pid_t pid = fork();if (pid < 0){cerr << "fork error!" << endl;return -1;}else if (pid > 0){cout << "I'm the parent process! Now write to the pipe!" << endl;close(pipefd[0]);//关闭管道的读取端char sendMsg[100] = "heiheihei";//这是要传递的信息write(pipefd[1], sendMsg, 100);//将信息写入管道close(pipefd[1]);//写完之后关闭写入端}else if (pid == 0){cout << "I'm the child process!";close(pipefd[1]);//过程和写入是类似的char receiveMsg[100];read(pipefd[0], receiveMsg, 100);//如果管道为空,这个函数默认被阻塞close(pipefd[0]);cout << "Now I've recerive the message:" << receiveMsg << endl;}waitpid(pid, NULL ,0); //包含于<sys/wait.h>,等待子进程结束并清理其资源return 0;}
控制台的输出结果如下:父进程发送的字符串在子进程中打印了:
I'm the parent process! Now write to the pipe!I'm the child process! Now I've recerive the message:heiheihei
如果要设置管道为非阻塞模式,可以通过下面两个函数实现:
fcntl(filedes[0], F_SETFL, O_NONBLOCK); fcntl(filedes[1], F_SETFL, O_NONBLOCK);
0 0
- LinuxC/C++编程(4)—管道通信
- LinuxC/C++编程(8)—共享内存通信
- LinuxC/C++编程(10)—socket本地通信
- LinuxC/C++编程(6)—重定向、管道重定向
- LinuxC/C++编程基础(3) 基于管道的进程间通信机制
- linuxc编程一站式学习(c基础)
- linuxc编程一站式学习(c CodingStyle)
- Linux C编程--进程间通信(IPC)4--管道详解
- Linux C编程--进程间通信(IPC)4--管道详解
- LinuxC编程一站式学习(C语言本质)
- 网络编程——进程间通信(匿名管道)
- Linux C——管道通信
- linux C使用管道通信(一)
- (11)《基于Linux的C编程与内核导读》--进程间管道通信(一)
- linux下c编程进程通信-管道与信号
- LinuxC 网络编程(一)
- LINUXC编程 一、C语言入门
- LinuxC/C++编程(1)—文件操作
- 机器学习该怎么入门?
- Linux系统下openCV的配置安装及测试
- springMVC 静态资源
- 为什么实时时钟的晶振都是32.768KHZ呢
- android系统裁减之服务裁减
- LinuxC/C++编程(4)—管道通信
- LabelReader.cpp:54:9: error: 'transform' is not a member of 'std'
- Caused by: org.hibernate.hql.ast.QuerySyntaxException: TkltEmpQuitProcess is not mapped. 问题解决
- 第13周——多态性和实现 (1)
- JDBC
- MySql在Linux的启动方式
- GStreamer基础教程05——集成GUI工具
- mac内存不足的时候
- hibernate要点