当前位置:网站首页>多进程编程(二):管道
多进程编程(二):管道
2022-07-02 23:05:00 【HDD615】
进程间通信(InterProcess Communication,IPC)
主要通信方式:
- 管道
1、匿名管道(pipe
)
2、有名管道(FIFO
) - 消息队列
- 共享内存
- 信号量
- 信号
- 套接字(
Socket
)
管道
匿名管道
前一章提到了一个shell命令:ps -ef | grep demo
,
这里的 |
其实就是一个管道,shell创建了两个进程来分别执行 ps -ef
和 grep demo
,并将前一个的输出,作为输入给到第二个。
特点:
1、管道是一个在内核内存中维护的缓冲区,这个缓冲区的存储能力是有限的,不同操作系统的大小不一定相同(Linux64位系统下其大小是4k),可以使用shell
命令:ulimit -a
查看
2、管道拥有文件的特质:读操作、写操作,但是没有文件实体。
3、管道只能承载无格式字节流以及缓冲区大小受限
4、通过管道传递的数据是顺序的,读取与写入的顺序保持一致
5、传递是单向的,如果想要双向通信,就要创建两个管道,也就是要么父进程写入,子进程读取;要么父进程读取,子进程写入
6、只能在具有公共祖先的进程之间使用(父子进程,兄弟进程,具有亲缘关系的进程)
函数
#include <unistd.h>
int pipe(int pipefd[2]);
- pipefd[0] : 管道读取端
- pipefd[1] : 管道写入端
- 返回值:
创建成功返回 0
创建失败返回 -1
举例:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main() {
// 创建管道
int pipe_fd[2] = {
0};
int res_p = pipe(pipe_fd);
if (res_p == 0) {
printf("pipe create success\n");
} else {
perror("pipe");
}
// 创建子进程
pid_t res = fork();
// 父进程写入数据
if (res > 0)
{
close(pipe_fd[0]); // 关闭读取端
printf("Parent: \n");
char buf_w[] = "Hello, world";
write(pipe_fd[1], buf_w, strlen(buf_w));
}
else if (res == 0) // 子进程读取数据
{
close(pipe_fd[1]); // 关闭写入端
printf("Child: \n");
char buf_r[100];
read(pipe_fd[0], buf_r, 100);
printf("%s\n", buf_r);
}
else
{
perror("fork");
}
return 0;
}
有名管道
匿名管道只能用于具有亲缘关系的进程间通信。为了客服这个缺点,提出了有名管道(FIFO)。
有名管道(FIFO)不同于匿名管道之处在于它提供了一个路径名与之关联,以FIFO的文件形式存于文件系统中,并且其打开方式与打开一个普通文件是一样的,这样即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够通过FIFO相互通信。
函数:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
- pathname: FIFO文件的路径 或者是 想要保存的路径
- mode: 权限
- 返回值:
成功:0
失败:-1
一旦创建了FIFO
,就可以使用 open
打开它,常见的 I/O
函数都可用于 FIFO
边栏推荐
- 附加:token;(没写完,别看…)
- 国外的论文在那找?
- Bloom filter
- Install docker and use docker to install MySQL
- Angled detection frame | calibrated depth feature for target detection (with implementation source code)
- Missing number
- Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
- 关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
- Sysdig analysis container system call
- Interface difference test - diffy tool
猜你喜欢
Mutual exclusion and synchronization of threads
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
附加:token;(没写完,别看…)
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
redis21道经典面试题,极限拉扯面试官
In February 2022, the ranking list of domestic databases: oceanbase regained its popularity with "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
Architecture: load balancing
实用系列丨免费可商用视频素材库
随机推荐
35页危化品安全管理平台解决方案2022版
接口自动化覆盖率统计——Jacoco使用
Use of cocospods
【单片机项目实训】八路抢答器
Question e: merged fruit -noip2004tgt2
Explain in detail the process of realizing Chinese text classification by CNN
关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
经济学外文文献在哪查?
TypeError: Cannot read properties of undefined (reading ***)
SQL query statement parameters are written successfully
Mutual exclusion and synchronization of threads
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Several methods of the minimum value in the maximum value of group query
Realization of mask recognition based on OpenCV
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
Custom throttling function six steps to deal with complex requirements
写论文可以去哪些网站搜索参考文献?
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
哪些软件可以整篇翻译英文论文?