当前位置:网站首页>多进程编程(二):管道
多进程编程(二):管道
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
边栏推荐
- Returns the maximum distance between two nodes of a binary tree
- 接口自动化覆盖率统计——Jacoco使用
- MFC gets the current time
- 论文的设计方案咋写?
- Which websites can I search for references when writing a thesis?
- Pytorch 20 realizes corrosion expansion based on pytorch
- Mutual exclusion and synchronization of threads
- Chapter 4 of getting started with MySQL: data types stored in data tables
- How much do you know about synchronized?
- Architecture: database architecture design
猜你喜欢

What are the recommended thesis translation software?

redis21道经典面试题,极限拉扯面试官
![Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration](/img/a3/55bb71d39801ceeee421a0c8ded333.png)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration

Many to one, one to many processing

基于OpenCV实现口罩识别

Chapter 3 of getting started with MySQL: database creation and operation

Bloom filter

Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect

Pytorch里面多任务Loss是加起来还是分别backward?

setInterval定时器在ie不生效原因之一:回调的是箭头函数
随机推荐
How QT exports data to PDF files (qpdfwriter User Guide)
Slf4j + Logback日志框架
Custom throttling function six steps to deal with complex requirements
Container runtime analysis
Interface automation coverage statistics - used by Jacobo
Architecture: load balancing
Program analysis and Optimization - 9 appendix XLA buffer assignment
95页智慧教育解决方案2022
Installing redis under Linux
Xcode real machine debugging
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
Where can I find foreign papers?
JS interviewer wants to know how much you understand call, apply, bind no regrets series
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Chapter 4 of getting started with MySQL: data types stored in data tables
Implement the foreach method of array
What website can you find English literature on?
TypeError: Cannot read properties of undefined (reading ***)
Feature Engineering: summary of common feature transformation methods