Linux IPC: shared memory

  This article introduces another way of inter-process communication: shared memory.

First, the understanding of shared memory

What is shared memory?

  Shared memory is to apply for a piece of physical memory for data sharing. The process that needs to share data can map the same piece of physical memory to its own virtual address space, and then directly access this space through its own virtual address space, so as to realize data sharing. comminicate.

The difference between shared memory and pipelines:

 (1) The communication type of the pipeline is transmission, and the data is first-in-first- out, and the data will disappear after being read, so the data will not be overwritten .

 (2) Shared memory is covered, so if multiple processes operate on shared memory, there will be security risks. But shared memory is also the fastest way to communicate between processes.

The difference between shared memory and pipeline in data transmission efficiency:

(1) Pipeline: The data undergoes two copies of data between the kernel space and the user space  during the transmission process .

 (2) Shared memory: The process directly accesses the shared memory through the virtual address space, and does not need to copy data twice between the kernel space and the user space .

  As shown in the figure: if a pipeline is used for data transmission between process A and process B, then first process A must send a copy of the data from the user space to the pipeline in the kernel space. Then process B needs to copy a copy of the data from the kernel space to the user space so that it can be used. Therefore, the pipeline transfers a data to go through two copies between the kernel space and the user space.

pipeline

  As shown in the figure: shared memory is a space applied for in physical memory. Two processes that need to share data establish a mapping relationship with this shared memory, and then they can directly read data from shared memory without going through it twice. Copy, and thus the fastest way to communicate between processes.

Shared memory transfer method

2. Shared memory operation process

  1. Create or open shared memory:

    • To create a shared memory is to open up a piece of physical memory to be used as a shared memory, and to open a shared memory is to open a shared memory that has already been opened.
  2. Map shared memory into virtual address space:

    • If a process wants to use this shared memory, it must map the shared memory to the virtual address space of the process, so that the process can access this shared memory through the virtual address space.
  3. Shared memory operations:

    • Operating shared memory is to use shared memory to communicate, that is, to write data to shared memory or to read data from shared memory.
  4. Unmap

    • After the shared memory is used up, the mapping relationship between the shared memory and the process virtual space needs to be released first.
  5. delete shared memory

    • After the mapping relationship is released, the shared memory can be deleted.

  When deleting shared memory, there is one more thing to pay attention to: the number of mapped connections ( the number of processes that map to this shared memory ).

  After the process cancels the mapping relationship with the shared memory, the number of mapping connections is reduced by 1. However, a shared memory is usually operated by multiple processes (such as process A and process B). After process A executes the operation of deleting the shared memory, the shared memory will not be deleted immediately, otherwise process B will use it well. Suddenly it broke.

  Therefore, after the operation of deleting the shared memory is performed, the shared memory cannot be deleted immediately, but the shared memory will be marked as deleted . If other processes want to connect to this shared memory in the future, the system will find that the shared memory is deleted. Connections from other processes have been rejected.

  After the processes connected to this shared memory are unmapped from this shared memory, the number of mapped connections becomes 0, and the system will release the shared memory at this time.

3. Shared memory operation interface

 Introduce the operation interface of shared memory in sequence according to the operation process of shared memory.

1. Create/open shared memory

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

 Role: create or open shared memory

  • ket_t key: The identifier of the shared memory, that is, the name of the shared memory (note the distinction between the identifier and the file identifier).

  • size_t size: The size of the shared memory to be created.

  • int shmflag: Creation permission/open method. If it is to create a shared memory, it is the creation permission, and if it is to open the shared memory, it is the opening method. The opening method is usually IPC_CREAT (create shared memory if it does not exist)

 Return value: returns the operation handle of the shared memory (non-negative integer) if successful, returns -1 if failed

About key:
  Multiple processes find the same shared memory by the name of the shared memory. (If the name is named IPC_PRIVATE, it means that this shared memory can only be used for inter-process communication with affinity, because other processes cannot find this shared memory)

Regarding size:
  The size of shared memory and the occupied space of shared memory are not the same thing. The development of the shared memory space is based on the memory page. It does not mean that if you want to create a shared memory of 10 bytes, you will open up a shared memory space with a size of 10 bytes. Because memory and disk space management is managed in units of blocks, not in units of bytes.

  That is to say, if you want to create a shared memory with a size of 10 bytes, you will actually open up a shared memory with a size of 4096 bytes, but the size used is 10 bytes.

2. Establish a mapping relationship between the process and the shared memory

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

 Role: establish a mapping relationship

  • shmid: The operation handle of the shared memory to be mapped.
  • addr: Specifies the first address of the mapping, that is, where the shared memory is mapped to the virtual space, usually set to NULL (let the system choose the appropriate location by itself).
  • shmflag: The operation to be performed on the shared memory, usually 0 (readable and writable), or SHM_RDONLY (read-only)

 Return value: If the establishment is successful, return the first address of the mapped space (operate the shared memory through this address), and return (void*)-1 if it fails.

map location

3. Operating shared memory

  Because shared memory is actually a piece of memory, we can directly use functions that operate on memory to operate shared memory, such as: memcpy, strcpy, printf

4. The process unmaps the shared memory

int shmdt(void* shm_start)

 Function: Release the mapping relationship between the process and the specified shared memory

  • shm_start: The mapping first address of the shared memory in the virtual space.

5. Delete shared memory

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

 Function: Delete shared memory (this function is not only used to delete, it has many functions, but we often use the delete function)

  • int shmid: The operation handle of the shared memory to be operated.
  • int cmd: The operation to be performed on the shared memory. We usually use IPC_RMID (meaning delete shared memory)
  • struct shmid_ds* buf: used to set or get shared memory information, set NULL if not used.

 Return value: The return value is actually not fixed, because different operation options represent different functions, and different functions have different return values. For delete operations, it returns 0 on success and -1 on failure.

4. Shared memory related instructions

1. View shared memory information

  • ipcs -m

 As shown in the figure: At this time, there is only one shared memory in the system.

  • key: the name of the shared memory
  • shmid: shared memory id
  • owner: the owner of the shared memory
  • perms: permissions
  • bytes: shared memory size
  • nattch: the number of mapping connections
  • status: status of shared memory

View shared memory information

2. Delete the specified shared memory

  • ipcrm -m shmid

  Note that the state of the shared memory at this time changes to dest (deleted state), and after becoming this state, other processes are not allowed to establish mappings with it.

delete shared memory

Guess you like

Origin blog.csdn.net/weixin_57761086/article/details/127868002