当前位置:网站首页>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
边栏推荐
- Where can I find the English literature of the thesis (except HowNet)?
- CMake基本使用
- Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
- 多进程编程(二):管道
- Pytorch里面多任务Loss是加起来还是分别backward?
- Xcode real machine debugging
- 秒杀系统设计
- MFC 获取当前时间
- Feature Engineering: summary of common feature transformation methods
- MySQL advanced learning notes (4)
猜你喜欢
Chapter 3 of getting started with MySQL: database creation and operation
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
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
MySQL 23道经典面试吊打面试官
Explain in detail the process of realizing Chinese text classification by CNN
MATLAB signal processing [Q & a notes-1]
Markdown使用教程
Custom throttling function six steps to deal with complex requirements
95 pages of smart education solutions 2022
多进程编程(一):基本概念
随机推荐
Bypass AV with golang
Using tensorflow to realize voiceprint recognition
Seckill system design
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
MFC 获取当前时间
NC50528 滑动窗口
Interface automation coverage statistics - used by Jacobo
S12. Verify multi host SSH mutual access script based on key
多进程编程(二):管道
Chapter 4 of getting started with MySQL: data types stored in data tables
Is there a specific format for English papers?
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
Custom throttling function six steps to deal with complex requirements
setInterval定时器在ie不生效原因之一:回调的是箭头函数
布隆过滤器
Question e: merged fruit -noip2004tgt2
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Explain in detail the process of realizing Chinese text classification by CNN
redis21道经典面试题,极限拉扯面试官