当前位置:网站首页>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
边栏推荐
- Markdown使用教程
- MySQL advanced learning notes (4)
- Sysdig analysis container system call
- Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
- Question e: merged fruit -noip2004tgt2
- 哪些软件可以整篇翻译英文论文?
- CMake基本使用
- CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
- What is the standard format of a 2000-3000 word essay for college students' classroom homework?
猜你喜欢

哪些软件可以整篇翻译英文论文?

開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開

Mutual exclusion and synchronization of threads

Architecture: database architecture design

95 pages of smart education solutions 2022

多进程编程(二):管道

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

Sysdig analysis container system call

95页智慧教育解决方案2022

What are the recommended thesis translation software?
随机推荐
sysdig分析容器系统调用
JVM foundation review
v8
AcWing_ 188. Warrior cattle_ bfs
Happy Lantern Festival, how many of these technical lantern riddles can you guess correctly?
布隆过滤器
Maya fishing house modeling
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
接口差异测试——Diffy工具
多进程编程(一):基本概念
Shell脚本基本使用
Several methods of the minimum value in the maximum value of group query
基于OpenCV实现口罩识别
Missing number
MFC gets the current time
Many to one, one to many processing
Custom throttling function six steps to deal with complex requirements
Additional: token; (don't read until you finish writing...)