当前位置:网站首页>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
边栏推荐
- Several methods of the minimum value in the maximum value of group query
- JS interviewer wants to know how much you understand call, apply, bind no regrets series
- redis21道经典面试题,极限拉扯面试官
- 写论文可以去哪些网站搜索参考文献?
- Understanding and application of least square method
- Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
- 请问大家在什么网站上能查到英文文献?
- Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
- 有哪些比较推荐的论文翻译软件?
- [shutter] shutter open source project reference
猜你喜欢
Interface difference test - diffy tool
布隆过滤器
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
【单片机项目实训】八路抢答器
Should you study kubernetes?
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
Pytorch里面多任务Loss是加起来还是分别backward?
写论文可以去哪些网站搜索参考文献?
What are the recommended thesis translation software?
Xcode real machine debugging
随机推荐
redis21道经典面试题,极限拉扯面试官
Go custom sort
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表
来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
How QT exports data to PDF files (qpdfwriter User Guide)
Xcode real machine debugging
Returns the size of the largest binary search subtree in a binary tree
sysdig分析容器系统调用
Where can I check the foreign literature of economics?
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Sysdig analysis container system call
MFC 获取当前时间
MATLAB signal processing [Q & a notes-1]
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
英文论文有具体的格式吗?
NC50528 滑动窗口
JS interviewer wants to know how much you understand call, apply, bind no regrets series
TypeError: Cannot read properties of undefined (reading ***)
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?
Analyze ad654: Marketing Analytics