[Inter-process communication] Anonymous pipes, named pipes and shared memory related knowledge points

One, inter-process communication

1. What is inter-process communication?

Inter-process communication refers to the exchange of data between supporting processes.

2. Why do I need to communicate between processes?

Each process has its own independent process virtual address space, which promotes the independence of the process. However, it also brings about the problem of process and cooperation between processes. Currently, the largest inter-process communication is the network.

3. The purpose of inter-process communication

Data transmission : A process needs to send its data to another process.
Resource sharing : The same resources are shared among multiple processes.
Notification event : A process needs to send a message to another process or a group of processes to notify it (they) that an event has occurred (such as notifying the parent process when the process terminates).
Process control : Some processes want to completely control the execution of another process (such as the Debug process). At this time, the control process hopes to intercept all traps and exceptions of another process, and be able to know its state changes in time.

Two, anonymous pipeline

1. What is a pipe?

A pipe is a piece of memory in the kernel, which is equivalent to a data buffer opened by the kernel for inter-process communication.

2. Anonymous pipe interface

int pipe(int fd[2])

fd[2]: is an output parameter. Define fd[2] before calling the pipe function, and assign values ​​inside the pipe function. After the pipe function is executed, operate the pipe through fd[2].
Among them, fd[2] includes:
fd[0]: indicates the read end, the pipeline can be read through fd[0]
fd[1]: indicates the write segment, and the pipeline can be written through fd[1]
Insert picture description here
Return value: successful return 0, return -1 on failure.

3. Properties of anonymous pipes

a. An anonymous pipe is a piece of memory without an identifier created in the kernel.
The b.pipe function will return two file descriptors for programmers to read and write to the memory.
c. The data flow of the pipeline can only flow from the writing section to the reading end, and the data flow is single, which is called half-duplex.
d. Anonymous pipes are only suitable for processes that are related.
Insert picture description here
In the above figure, the parent process currently calls pipe to create an anonymous pipe. There will be fd[0] and fd[1] in the file descriptor table of the parent process, and then when creating the child process, the child process copies the PCB of the parent process, so There will also be newly created fd[0] and fd[1] in the file description table of the child process. At this time, both processes can read and write the memory opened by the anonymous pipe.

4. Byte stream service for anonymous pipes

The data is divided into bytes for transmission. During the transmission, the receiver can choose any number of bytes to accept.
Insert picture description here
Reading away is equivalent to taking data from the pipeline, and the pipeline does not have this data.
The size of the anonymous pipe is 64K, and the life cycle follows the process.

5. Non-blocking properties

First set the file descriptor as a non-blocking attribute

int fcntl(int fd,int cmd,...)
//cmd:
//F_GETFL:获取文件描述符的属性
//F_SETFL:设置文件描述符的属性

Then what are the characteristics when reading and writing?
5.1 The file descriptor on the writing side is set to non-blocking
a. When the reading side is not closed, the writing side keeps writing until the pipe is full, and then calling write, write returns -1, and an error is reported, and the resource is unavailable.
b. When the reader is closed and the writer writes, the process calling the write function will receive a SIGPIPE signal, causing the process to exit.
5.2 The file descriptor at the read end is set to non-blocking
a. When the write end is not closed, the read end keeps reading until the content in the pipeline is finished, read will return -1, and an error is reported at the same time, and the resource is unavailable.
b. When the write end is closed, the read end keeps reading until the content in the pipe is read, read will return 0, indicating that 0 bytes have been read.

Three, named pipe

1. What is a named pipe?

A named pipe is also a piece of memory in the kernel, or a buffer, but this piece of memory has an identifier.

2. Create a named pipe

Named pipes can be created from the command line, the command line method is to use the following command:

mkfifo filename

Named pipes can also be created from programs, related functions are:

int mkfifo(const char *filename,mode_t mode); 

In fact, a named pipe file (named pipe identifier) ​​is created. When different processes open such a file, read and write to the file, that is, read and write the memory in the kernel, thus realizing communication between different processes. .

3. Features of named pipes

3.1 Has an identifier
3.2 The life cycle of a named pipe follows the process
3.3 Other features are consistent with anonymous pipes

4. The difference between anonymous and named pipes

4.1 Anonymous pipes are created and opened by the pipe function
4.2 Named pipes are created by the mkfifo function and opened with open
4.3 The only difference between FIFO (named pipe) and pipe (anonymous pipe) is the way they are created and opened, but these work After completion, they have the same semantics

Fourth, shared memory

1. The principle of shared memory

Insert picture description here
1. Shared memory opens up a space on the physical memory.
2. Different processes can map the opened physical memory to their own shared area through page table mapping
. 3. Different processes operate the virtual address in the shared area in the code. In order to achieve the operation of physical memory, so as to achieve inter-process communication between different processes

2. Shared memory interface

2.1 Get shared memory

int shmget(key_t key,size_t size,int shmflg)

key: Shared memory identifier, as long as it does not conflict with the memory identifier in the current system, it
is
fine. When passing parameters, you can give a hexadecimal number size: the size of the shared memory shmflg: IPC_CREAT, when the shared memory is not exist, create; IPC_CREAT | IPC_EXCL, get to create their own shared memory, shared memory If, at the time, find the identifier of the shared memory already exists, the error
returns: the operating handle shared memory

2.2 Attachment to process

void* shmat(int shmid,const void* shmaddr,int shmflg)

shmid: Shared memory operation handle
shmaddr: To attach to the shared memory, pass the value NULL, indicating that the operating system selects shared memory.
shmflg: 0 means read and write, IPC_RDONLY means read only
Return value: return the additional address in the shared memory.
Note :
1. When operating the shared memory, when writing to the shared memory, it is the way of overwriting.
2. When operating the shared memory, when reading from the shared memory, the data is accessed instead of read.

The life cycle of shared memory follows the kernel
command: ipcs
Insert picture description here
command: ipcrm -m [shared memory operation handle]
If a shared memory is deleted:
1. The identifier of the shared memory will be set to 0x00000000, indicating that the current shared memory cannot be used, and The state is also set to dest
2. If it is deleted, there is still a process attached, but the shared memory block has been released, so if the attached process uses the deleted shared memory, it may cause a crash
3. If there is no process attached, when using ipcrm -m to delete, it will be deleted directly

2.3 Separation

int shmdt(const void* shmaddr)

shmaddr: return value of shmat, address of shared area

2.4 Interface for operating shared memory

int shmctl(int shmid,int cmd,struct shmid_ds* buf)

shmid: Shared memory operation handle
cmd: Tell shmctl what to do. IP_STAT: Get the attributes of shared memory, use it with the third parameter buf (out parameter); IP_SET: set the attributes of shared memory, use it with the third parameter buf (input parameter); IPC_RMID: delete shared memory, third A parameter can be passed NULL
buf: the operating system describes a structure of shared memory

Guess you like

Origin blog.csdn.net/zhao_leilei/article/details/109187114