当前位置:网站首页>多进程编程(二):管道
多进程编程(二):管道
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
边栏推荐
- [shutter] shutter open source project reference
- What website can you find English literature on?
- yolov5detect. Py comment
- 容器运行时分析
- 在线预览Word文档
- Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- S12. Verify multi host SSH mutual access script based on key
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- 35 pages dangerous chemicals safety management platform solution 2022 Edition
猜你喜欢
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
Matlab 信号处理【问答笔记-1】
setInterval定时器在ie不生效原因之一:回调的是箭头函数
实用系列丨免费可商用视频素材库
Digital collection trading website domestic digital collection trading platform
CMake基本使用
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
TypeError: Cannot read properties of undefined (reading ***)
JDBC tutorial
RTP 接发ps流工具改进(二)
随机推荐
Many to one, one to many processing
95 pages of smart education solutions 2022
What are the recommended thesis translation software?
Interface automation coverage statistics - used by Jacobo
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
MFC file operation
Where can I find foreign papers?
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
Realization of mask recognition based on OpenCV
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
Bloom filter
Cmake basic use
Chapter 4 of getting started with MySQL: data types stored in data tables
Markdown使用教程
SQL query statement parameters are written successfully
Use of cocospods
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
MySQL advanced learning notes (III)
Go custom sort