当前位置:网站首页>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 )
边栏推荐
- Working principle and application of fast recovery diode
- 2、 Internationally renowned project HelloWorld
- minipcie接口CAN卡解决工控机扩展CAN通道的难题 minipcie CAN
- 创建MySQL数据库的两种方式
- autocomplete禁止表单自动填充
- Can serial port can 232 can 485 serial port to CANbus bus gateway module can232/485mb converter cancom
- Stm32fsmc extended SRAM
- UE4 two ways to obtain player control
- [cloud native | 17] four network modes of container
- Build a maker Education Laboratory for teenagers
猜你喜欢

创建MySQL数据库的两种方式

【语义分割】2018-DeeplabV3+ ECCV

Vector explanation and iterator failure

The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)](/img/a0/b2b0f5fb63301f5b7dd14302aa39e2.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)

FFmpeg 视频添加水印

SQL加解密注入详解

Steam science education endows classroom teaching with creativity

STM32开发 | AD7606并行多路采集数据

UE4 switching of control rights of multiple roles
随机推荐
一个sql server查询截止某个日期最新的记录
十、拦截器
[mathematical modeling] basic knowledge of MATLAB
idea插件离线安装(持续更新)
Torch slice maintenance
Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
What are the restrictions on opening futures accounts? Where is the safest place to open an account?
Rotate array minimum number
Customer service relationship management based on SQL net enterprise messenger enterprise communications
一、基础入门
Autocomplete prevents the form from automatically filling in
Weights & biases (II)
data warehouse
2022 Henan Mengxin League game (3): Henan University L - synthetic game
Is this my vs not connected to the database
解决 Incorrect string value: ‘\xF0\x9F\x98\xAD“,...‘ for column ‘commentContent‘ at row 1 报错
UE4 多个角色控制权的切换
Spark Structured Streaming HelloWorld
快恢复二极管工作原理及使用
autocomplete禁止表单自动填充
