Linux interprocess communication shared memory mode

  

Shared memory mode: Take part of the physical memory and share it as multiple processes.

Shared memory is the fastest way to share data between processes. A process writes data to a shared memory area, and all processes sharing the memory can see the contents immediately.

Shared Memory Implementation Steps:

First, create shared memory, use the shmget function.

Second, map shared memory, map the created shared memory to the specific process space, use the shmat function.

Creating shared memory shmget:

intshmget(key_t key, size_t size, int shmflg)

Function: Get a shared memory identifier or create a shared memory object and return Shared memory identifier.

key: 0(IPC_PRIVATE) will create a shared memory object

size: an integer greater than 0, the size of the new shared memory, in bytes. When only shared memory is obtained, it is specified as 0.

shmflg:

0 means to take the shared memory identifier, if it does not exist, the function will report an error;

IPC_CREAT, if the kernel If there is no shared memory with the same key value and key, then create a shared memory; if there is such shared memory, return the identifier of the shared memory;

IPC_CREAT

Copyright © Windows knowledge All Rights Reserved