当前位置:网站首页>Multiprocess programming (II): Pipeline
Multiprocess programming (II): Pipeline
2022-07-03 00:22:00 【HDD615】
Interprocess communication (InterProcess Communication,IPC)
Main communication mode :
- The Conduit
1、 Anonymous pipeline (pipe)
2、 Famous pipeline (FIFO) - Message queue
- Shared memory
- Semaphore
- The signal
- Socket (
Socket)
The Conduit
Anonymous pipeline
The previous chapter mentioned a shell command :ps -ef | grep demo,
there | It's actually a pipe ,shell Two processes are created to execute ps -ef and grep demo, And output the previous one , As input to the second .
characteristic :
1、 A pipeline is a buffer maintained in kernel memory , The storage capacity of this buffer is limited , The size of different operating systems is not necessarily the same (Linux64 The size of the bit system is 4k), have access to shell command :ulimit -a see
2、 The pipeline has the characteristics of the file : Read operations 、 Write operations , But there is no file entity .
3、 Pipes can only carry unformatted byte streams and the buffer size is limited
4、 The data passed through the pipeline is sequential , The order of reading and writing is consistent
5、 Delivery is one-way , If you want two-way communication , Just create two pipes , That is, either the parent process writes , Subprocess read ; Either the parent process reads , Subprocess write
6、 Can only be used between processes with a common ancestor ( Father child process , Brotherhood process , A process of kinship )
function
#include <unistd.h>
int pipe(int pipefd[2]);
- pipefd[0] : Pipe reading end
- pipefd[1] : Pipeline write end
- Return value :
Create successfully return 0
Creation failure returns -1
give an example :
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Create pipes
int pipe_fd[2] = {
0};
int res_p = pipe(pipe_fd);
if (res_p == 0) {
printf("pipe create success\n");
} else {
perror("pipe");
}
// Create child process
pid_t res = fork();
// The parent process writes data
if (res > 0)
{
close(pipe_fd[0]); // Close the reader
printf("Parent: \n");
char buf_w[] = "Hello, world";
write(pipe_fd[1], buf_w, strlen(buf_w));
}
else if (res == 0) // Subprocesses read data
{
close(pipe_fd[1]); // Close the write side
printf("Child: \n");
char buf_r[100];
read(pipe_fd[0], buf_r, 100);
printf("%s\n", buf_r);
}
else
{
perror("fork");
}
return 0;
}
Famous pipeline
Anonymous pipes can only be used for interprocess communication with kinship . For the disadvantage of customer service , The famous pipeline (FIFO).
Famous pipeline (FIFO) Unlike anonymous channels It provides a pathname associated with it , With FIFO In the file system , And its opening method is the same as opening an ordinary file , So even with FIFO The creation process of does not have a kinship process , As long as the path is accessible , I can get through FIFO Mutual communication .
function :
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
- pathname: FIFO Path to file Or is it The path you want to save
- mode: jurisdiction
- Return value :
success :0
Failure :-1
Once created FIFO, You can use open To open it , common I/O Functions can be used for FIFO
边栏推荐
- 多进程编程(四):共享内存
- Xcode real machine debugging
- [shutter] open the third-party shutter project
- 洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
- [shutter] shutter open source project reference
- setInterval定时器在ie不生效原因之一:回调的是箭头函数
- 開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
- 在线预览Word文档
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
猜你喜欢

MySQL advanced learning notes (III)
![[shutter] open the third-party shutter project](/img/1a/e35d0180612d7e79b55e7818193740.jpg)
[shutter] open the third-party shutter project

What are the projects of metauniverse and what are the companies of metauniverse

QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
![[target detection] r-cnn, fast r-cnn, fast r-cnn learning](/img/f0/df285f01ffadff62eb3dcb92f2e04f.jpg)
[target detection] r-cnn, fast r-cnn, fast r-cnn learning

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

Where can I find the English literature of the thesis (except HowNet)?
![[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)](/img/f7/a8eb8e40b9ea25021751d7150936ac.jpg)
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)

Which websites can I search for references when writing a thesis?

Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
随机推荐
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
Where can I find foreign papers?
多进程编程(一):基本概念
[shutter] shutter open source project reference
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
Install docker and use docker to install MySQL
免费自媒体必备工具分享
接口自动化覆盖率统计——Jacoco使用
MFC file operation
Which websites can I search for references when writing a thesis?
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
Pat 1030 travel plan (30 points) (unfinished)
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
35 pages dangerous chemicals safety management platform solution 2022 Edition
Is the multitasking loss in pytoch added up or backward separately?
Digital twin visualization solution digital twin visualization 3D platform
MFC gets the current time
MFC 获取当前时间