If you can delimit a piece of physical memory so that multiple processes can map that memory to its own virtual memory space, the process can communicate by reading and writing data to this memory space. In addition, unlike Message Queuing, where shared memory is in user space rather than in the kernel space, there is no "data replication between user space and kernel space" problem, which can reduce the overhead. Because different processes can read and write data to the same space, they require some synchronization mechanisms to prevent confusion, and the mechanisms available are "semaphores", "File locks," and so on. The shared memory has mmap and system V shared memories, which is the latter. To create or open shared memory:intShmget (key_t key, size_t size,intSHMFLG); This is very similar to msgget for creating and opening message queues, which also uniquely identifies the memory being shared with a key_t type of key. The size parameter represents the amount of shared memory to be created, just like the malloc function. The SHMFLG parameter is the same as the SHMFLG parameter of the Msgget function: It has two meanings, one is that the parameter can be value ipc_create, which means that the request creates a new shared memory, or it can be ipc_create| A bitwise combination of IPC_EXCL, which indicates a request to create a new shared memory but if it already exists, an error. The second is that the parameter represents permission control, such as 666 means all can read and write. So if this argument is written as ipc_create| ipc_excl|666 indicates a request to create a new shared memory, but if it already exists, an error and a permission of 666 attach the shared memory to the process's own virtual memory space:void*shmat (intShmid,Const void*SHMADDR,intSHMFLG); Shmid parameter: Prepare attach shared Memory ID shmaddr parameter: To attach the shared memory to the process's own virtual space address, it needs to be understood together with the SHMFLG parameter. 1If the parameter passes a null pointer, the shared memory is attach to the first possible memory that is automatically selected by the system2If the parameter is not empty:2.1) If SHMFLG &Shm_rnd is not 0, then shared memory will be attach to this address: (Shmaddr-((uintptr_t) Shmaddr%Shmlba)). Where the Shm_rnd identity represents rounding alignment (round), Shmlba is the low-segment address of the shared memory, which is the boundary address of the attached shared memory. So the above formula means: Move the address specified shmaddr to the integer multiple of the boundary address. Conversely, if the SHMFLG&shm_rnd is 0, the address specified by SHMADDR is used directly. 2.2if (SHMFLG &shm_rdonly) is not 0, then the shared memory will become read-only (if Read permission is available), conversely, (SHMFLG &shm_rdonly) is 0, the shared memory can be read writable (if there is write permission) when the attach is successful, the first address in the process virtual space in the share will be returned, then you can use it as if you were working with normal memory. Attach reverse: Detach shared memory from the process's memory spaceintSHMDT (Const void*shmaddr); The SHMADDR parameter is the address on the original attach. The data structure (SHMID_DS) of the shared memory corresponds to the data structure (MSQID_DS) of the message queue, and the related operations are similar:structShmid_ds {structIpc_perm Shm_perm;/*Operation Perms*/ intShm_segsz;/*size of segment (bytes)*/__kernel_time_t Shm_atime; /*Last attach time*/__kernel_time_t Shm_dtime; /*Last Detach time*/__kernel_time_t Shm_ctime; /* Last Change time*/__kernel_ipc_pid_t Shm_cpid; /*pid of creator*/__kernel_ipc_pid_t Shm_lpid; /*pid of last operator*/unsigned ShortShm_nattch;/*No. of current attaches*/unsigned Shortshm_unused;/*compatibility*/ void*shm_unused2;/*ditto-used by DIPC*/ void*SHM_UNUSED3;/*Unused*/ }; intShmctl (intShmid,intCmdstructShmid_ds *buf); The first parameter represents the ID of the shared memory to be manipulated the CMD parameter represents the action to be performed, similar to the MSGCTL function:1Ipc_stat Gets the message Queue property information and places the information in buffer set by the 3rd parameter2Ipc_set Some properties of Message Queuing based on data in buffer, not all properties can be set, which is limited to: Ipc_perm.uid,ipc_perm.gid,ipc_perm.mode These kinds of3) Ipc_rmid Delete the message queue. Note: Both Ipc_set and ipc_rmid have appropriate permission checks, and only those processes that have permissions can perform the relevant actions
Linux process Learning notes-shared memory