当前位置:网站首页>Interprocess communication
Interprocess communication
2022-07-26 04:50:00 【A vegetable programmer】
- Skillfully use pipe Communicate between parent and child processes
- Skillfully use pipe Communication between brother processes
- Skillfully use fifo Conduct unrelated interprocess communication
- Use mmap Carry out blood related interprocess communication
- Use mmap Conduct unrelated interprocess communication
Linux In the environment , Process address spaces are independent of each other , Each process has a different user address space . The global variables of any process cannot be seen in another process , So processes and processes cannot access each other , To exchange data, you have to go through the kernel , Create a buffer in the kernel , process 1 Copy data from user space to kernel buffer , process 2 Then read the data from the kernel buffer , The mechanism provided by the kernel is called interprocess communication (IPC,InterProcess Communication).

The Conduit -pipe
- Function function :
Create a pipe
- The function prototype :
int pipe(int fd[2]);
- Function parameter :
If the function call succeeds ,fd[0] The reading end of the storage pipe ,fd[1] Store the write end of the pipe
- Return value :
- Successfully returns 0;
- Failure to return -1, And set up errno value .
The function call successfully returns the file descriptors of the read side and the write side , among fd[0] It's the reading end , fd[1] It's the writing end , Reading and writing data to the pipeline is done by using these two file descriptors , The essence of the read-write pipeline is to operate the kernel buffer .
Create step summary :
- Parent process call pipe Function to create a pipeline , Get two file descriptors fd[0] and fd[1], Point to the read end and write end of the pipeline respectively .
- Parent process call fork Create child process , Then the child process also has two file descriptors pointing to the same tube .
- The parent process closes the pipe reader , The child process closes the pipe writer . The parent process can write data to the pipeline , The subprocess reads the data from the pipeline , In this way, the communication between parent and child processes is realized .
How to set the pipe to non blocking
By default , The read and write ends of the pipe are blocked , To set the read or write side to non blocking , Can refer to
Take the test in the following three steps :
The first 1 Step : int flags = fcntl(fd[0], F_GETFL, 0);
The first 2 Step : flag |= O_NONBLOCK;
The first 3 Step : fcntl(fd[0], F_SETFL, flags);
If the reader is set to non blocking :
- The write side is not closed , There is no data readable in the pipeline , be read return -1;
- The write side is not closed , There is data readable in the pipeline , be read Returns the actual number of bytes read
- The writer has been turned off , There is data readable in the pipeline , be read Returns the actual number of bytes read
- The writer has been turned off , There is no data readable in the pipeline , be read return 0
How to view the pipeline buffer size
- command
ulimit -a
- function
long fpathconf(int fd, int name);
printf("pipe size==[%ld]\n", fpathconf(fd[0], _PC_PIPE_BUF));
printf("pipe size==[%ld]\n", fpathconf(fd[1], _PC_PIPE_BUF));
Create pipes
- The way 1- Use command mkfifo
Command format : mkfifo Pipe name
for example :mkfifo myfifo
- The way 2- Using functions
int mkfifo(const char *pathname, mode_t mode);
Parameter description and return value can be viewed man 3 mkfifo
When you create a FIFO, You can use open Function to open it , Common documents I/O Functions can be used for FIFO. Such as :close、read、write、unlink etc. .
FIFO Strictly follow the "first in, first out" principle (first in first out), Yes FIFO The read always returns data from the beginning , Writing them adds data to the end . They don't support things like lseek() Wait for file location operation
mmap function
- Function function :
Create a storage mapping area
- The function prototype
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
- Function return value :
- success : Return the first address of the created mapping area ;
- Failure :MAP_FAILED macro
- Parameters :
- addr: Specify the starting address of the mapping , Usually set to NULL, Specified by the system
- length: File length mapped to memory
- prot: Protection mode of mapping area , Most commonly used :
- read :PROT_READ
- Write :PROT_WRITE
- Reading and writing :PROT_READ | PROT_WRITE
- flags: Characteristics of the mapping area , It can be
- MAP_SHARED: The data written into the mapping area will be written back to the file , And allow other processes mapping the file to share .
- MAP_PRIVATE: Writing to the mapped area will produce a copy of the mapped area (copy-on-write), Changes made to this area will not be written back to the original file .
- fd: from open File descriptor returned , Represents the file to map .
- offset: Offset at the beginning of the file , Must be 4k Integer multiple , Usually it is 0, Indicates that the mapping starts from the file header .
- munmap function
- Function function :
Release from mmap Function to create a storage mapping area
- The function prototype :
int munmap(void *addr, size_t length);
- Return value :
success : return 0
Failure : return -1, Set up errno value
- Function parameter :
- addr: call mmap The first address of the mapping area successfully returned by the function
- length: Mapping area size (mmap The second parameter of the function )
边栏推荐
- 2022 Henan Mengxin League game (3): Henan University L - synthetic game
- Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication
- 【语义分割】2018-DeeplabV3+ ECCV
- Niuke-top101-bm32
- Calculate the curvature of discrete points (matlab)
- 人脸数据库收集总结
- Weights & Biases (二)
- UE4 switching of control rights of multiple roles
- 2022河南萌新联赛第(三)场:河南大学 A - 玉米大炮
- 2022河南萌新联赛第(三)场:河南大学 L - 合成游戏
猜你喜欢
随机推荐
有ggjj看看这个问题没,是否缓存导致跨域问题?
2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication
IEC61131 数据类型与 C#数据类型的对应
Optimization analysis and efficiency execution of MySQL
Stm32fsmc extended SRAM
【云原生 | 17】容器的四种网络模式
SQL加解密注入详解
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
Vector explanation and iterator failure
mongoDB为什么快
LeetCode - 单调栈与单调队列
UE4 keyboard control switch light
[mathematical modeling] basic knowledge of MATLAB
Phaser(一):平台跳跃收集游戏
二、国际知名项目-HelloWorld
minipcie接口CAN卡解决工控机扩展CAN通道的难题 minipcie CAN
An SQL server queries the latest records as of a certain date
Offline installation of idea plug-in (continuous update)
2022河南萌新联赛第(三)场:河南大学 J - 神奇数字










