System V shared memory of linux




                                          Shared memory
    Shared memory is a special address space created by the system, allowing multiple unrelated processes to use this memory space, that is, multiple processes can use the data in the same memory.

    Compared with other process communication methods, shared memory does not need to copy data and directly reads and writes memory, which is a very efficient process communication scheme. But it does not provide a synchronous access mechanism by itself, and we need to control it ourselves. In LINUX, as long as the shared memory segment is connected to the address space of the process, the process can access the addresses in the shared memory.

    The shared memory operation functions provided by the LINUX system are similar to semaphores, message queues, etc., mainly as follows:
  (1) int shmget(key_t key,int shmsz,int shmflg);
  (2) void *shmat(int shmid,const void *shmaddr, int shmflg);
   if shmaddr is 0 then this segment is linked to the first available address selected by the kernel, which is the recommended way to use
   if shmaddr is non-zero and SHM_RND is not specified, then this segment is linked to addr
   If shmaddr is non-zero and SHM_RND is specified at the specified address, the segment is linked to the address indicated by shmaddr - (addr mod ulus SHMLBA) . SHM_RND means low boundary address multiple, which is always a power of 2. The formula is to take the address down to the nearest multiple of SHMLBA
  (3) int shmdt (const void *shmaddr);
  (4) int shmctl(int shmid, int cmd,struct shmid_ds *buf);

The meanings of the above functions are as follows:
 The Shmget() function allocates a new shared memory. Shmsz specifies the size of the shared memory in bytes. The setting of shmflg is similar to the parameter semflg in the semget() function of the semaphore.
If the shmget() function is called successfully, it returns the ID of the shared memory; otherwise, it returns -1.

 The role of the Shmat() function is to connect the shared memory with the address space of a process. Shmid is the shared memory ID returned by the shmget() function. Shmaddr is the storage address where the shared memory is connected to the process. Generally, it is set to a null pointer, indicating that the system will complete the work. Shmflg sets the shared memory control options. There are two possible values: SHM_RND (related to the shmaddr parameter) and SHM_RDONLY (read only). Returns a pointer to shared memory if the shmat() function is called successfully; otherwise returns -1.

 The Shmdt() function is used to disassociate the process from the shared memory area, so that the current process cannot continue to access the shared memory. The parameter shmaddr is the pointer returned by the shmat() function. Returns 0 if the operation is successful; -1 if it fails.

 The Shmctl() function implements control operations on the shared memory area. Its usage is similar to the msgctl() function of the message queue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324735943&siteId=291194637