当前位置:网站首页>多进程编程(二):管道
多进程编程(二):管道
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
边栏推荐
- Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
- [reading notes] phased summary of writing reading notes
- 教育学大佬是怎么找外文参考文献的?
- Returns the size of the largest binary search subtree in a binary tree
- Where can I check the foreign literature of economics?
- 关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- [shutter] open the third-party shutter project
- Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
- MFC 获取当前时间
猜你喜欢

Angled detection frame | calibrated depth feature for target detection (with implementation source code)
![[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a](/img/f2/f3b8899aa774dd32006c5928d370f1.gif)
[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a

Digital twin visualization solution digital twin visualization 3D platform

论文的英文文献在哪找(除了知网)?
![[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)](/img/c5/2f65d37682607aab98443d7f1ba775.jpg)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)

67页新型智慧城市整体规划建设方案(附下载)

Matlab 信号处理【问答笔记-1】

基于OpenCV实现口罩识别

MySQL advanced learning notes (III)

Where can I find the English literature of the thesis (except HowNet)?
随机推荐
[reading notes] phased summary of writing reading notes
Go自定义排序
Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
zhvoice
LeedCode1480.一维数组的动态和
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
Using tensorflow to realize voiceprint recognition
接口自动化覆盖率统计——Jacoco使用
论文的设计方案咋写?
Architecture: load balancing
RTP 接发ps流工具改进(二)
MySQL 23道经典面试吊打面试官
Interface automation coverage statistics - used by Jacobo
Implement the foreach method of array
国外的论文在那找?
Basic 10 of C language: array and pointer
sysdig分析容器系统调用
Pytorch里面多任务Loss是加起来还是分别backward?
教育学大佬是怎么找外文参考文献的?