Linux interprocess communication-2

In the previous blog, we mentioned four ways of inter-process communication: pipeline, shared memory, message queue and semaphore, and explained the content of the pipeline. In this blog, we undertake the above and the next inter-process communication content.

Table of contents

1. Shared memory

1.1 Content

1.2 Operation

1.2.1 shmget interface

1.2.2 shmat interface

1.2.3 shmdt interface

1.2.4 shmctl interface

1.2.5 Application

1. Shared memory

1.1 Content

Shared memory is used for data sharing between multiple processes and is the fastest communication method among all inter-process communication methods.

The principle of shared memory is also very simple: open up a piece of physical memory, and then multiple processes map this piece of physical memory into their own page tables, and access the data in the physical address of this shared memory through their own virtual addresses.

For shared memory, processes can access it directly. Compared with the pipeline process that needs to copy the data into it (write), and then read and copy the data to its own memory (read) by other processes, there are two fewer copy processes, and the communication time is greatly reduced. Therefore, we say that shared memory is the fastest way to communicate between processes.

1.2 Operation

1.2.1 shmget interface

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

The function of the shmget interface is to create a shared memory, where key is the identifier (name) of the shared memory, and size is the size of the shared memory to be created, preferably an integer multiple of PAGE_SZIE (4096kb);

shmflg is a keyword: IPC_CREAT--create if the shared memory does not exist, open if it exists; IPC_EXCL--use with IPC_CREAT, create if the shared memory does not exist, and report an error if it exists; mode_flags--access permission of the shared memory 0664.

If shmget is created successfully, it returns a non-negative integer (operation handle); if it fails, it returns -1.

1.2.2 shmat interface

void* shmat(int shmid, void* addr, int shmflag);

The function of the shmat interface is to map the address information of the shared memory to the virtual address space in the process, where shmid is the operation handle returned by shmget when opening the shared memory, and addr is the first address of the mapping, but we generally set it to NULL, so that Assigned by the operating system to prevent human errors; shmflag: The default value is 0, which means readable and writable, and SHM_RDONLY means read-only (provided the permission is allowed).

If the shmat mapping is successful, it returns the first address of the mapping, and if it fails, it returns (void*)-1. After we get the first address, we can operate on the content in the shared memory.

1.2.3 shmdt interface

int shmdt(const void* shmaddr);

The shmdt interface is to release the mapping relationship between the shared memory and the process, where shmaddr is the first address of the mapping, which is the return value of the shmat interface.

If shmat contacts the mapping relationship successfully, it returns 0; if it fails, it returns -1.

1.2.4 shmctl interface

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

The shmctl interface is to realize the control of shared memory, and is generally used to mark the deletion of shared memory. Among them, shmid is the operation handle returned by the shmget interface; cmd is the operation on shared memory: IPC_RMID--marks a shared memory segment that needs to be deleted; buf is used to set or obtain shared memory information, and is ignored when cmd is IPC_RMID.

It is worth noting that RMID is called mark delete, not delete. This is because if multiple processes access the same shared memory, if one of the processes suddenly wants to delete the shared memory, deleting it directly will obviously cause other processes to access the wrong me.

So we mark the shared memory, and the marked shared memory no longer accepts new mappings until the current mapping connection count is 0, and the shared memory is actually deleted. That is to say, the interface is only marked for deletion, but the actual above is done by the operating system.

1.2.5 Application

Write programs as usual to practice and demonstrate the use and functions of the above interfaces.

First complete writing data to the shared memory:

Then read data to the shared content:

Finally, copy the terminal, one terminal writes data to the shared memory, and one terminal reads data from the shared memory, and the results are as follows:

 This result is a good example of the implementation of shared memory for inter-process communication.

Guess you like

Origin blog.csdn.net/m0_56821563/article/details/129984312