Linux environment interprocess communication: Shared memory

Source: Internet
Author: User

Introduction to Shared memory shared memory allows two or more processes to share a given store. This is the fastest IPC because the data does not need to replicate between the client process and the server process. There are two ways to share memory: mmap () system calls and System V shared memory. The mmap () system call MMAP () system call enables shared memory between processes by mapping the same common file. After the normal file is mapped to the process address space, the process can access the same file as the normal memory without having to call read (), write (), and so on.
Note: In fact, the mmap () system call is not designed entirely for shared memory. It itself provides a different way of accessing ordinary files than normal, and processes can operate on ordinary files like read-write memory. The shared memory IPC for POSIX or System V is purely for sharing purposes, and of course mmap () realizes shared memory is also one of its main applications.
The mmap () system call prototype is as follows:

The addr parameter is used to specify the starting address of the mapped store. It is usually set to 0, which means that the start address of the map area is selected by the system. The return address of this function is the starting address of the map area. FD Specifies the descriptor of the file to be mapped. Before you map the file to an address space, open the file first. Len is the number of bytes mapped. Offset is the starting offset to map bytes in the file. The PROT parameter describes the protection requirements for the mapped store, preferably with the following values or: Prot_read (readable), prot_write (writable), prot_exec (executable), Prot_none (inaccessible). The flags parameter affects several properties of the mapped store, specified by the following constant values: map_shared, Map_private, map_fixed, where map_shared, map_private is required, and map_fixed is not recommended. Mmap () Two ways to share memory (1) Use a memory map provided by a normal file: applies to any process; At this point, you need to open or create a file and then call Mmap (); The typical calling code is as follows:

(2) Use special files to provide anonymous memory mapping: Applies to relationships between processes; Because of the special affinity of a parent-child process, call Mmap () first in the parent process and then call Fork (). Then, after calling Fork (), the child process inherits the address space of the parent process after the anonymous mapping and also inherits the address returned by Mmap (), so that the parent-child process can communicate through the mapped area. Note that this is not a general inheritance relationship. In general, child processes maintain separate variables inherited from the parent process. The address returned by the mmap () is maintained by the parent-child process together.
The best way to implement shared memory for a genetically-related process should be to use anonymous memory mapping. At this point, you do not have to specify a specific file, just set the appropriate flag.
Mmap () system Call Summary 1, The content of the resulting mapped file does not exceed the initial size of the file itself, i.e. the mapping cannot change the size of the file;
2. The valid address space size that can be used for process communication is largely limited by the size of the mapped file, but not entirely limited to the file size.
Note: In Linux, the protection of memory is based on the page as the basic unit, even if the mapped file has only one byte size, the kernel assigns a page-size memory to the map. When the mapped file is less than a page size, the process can access a page size starting from the mmap () return address, without error, but if access to an address space other than a page causes an error to occur. Therefore, the effective address space available for interprocess communication does not exceed the size of the file and the size of a page.
3. Once the file is mapped, the process that calls Mmap () accesses the return address as an access to an area of memory that is temporarily detached from the effect of the file on disk. all operations on mmap () return address space are only meaningful in memory, the contents of the memory are written back to the disk file only after Munmap () or Msync () is called, and the content is still not larger than the size of the file.
System V Shared memory System V shared Memory Principle System V is the shared memory communication between processes by mapping files in the special file system SHM. That is, each shared memory region corresponds to a file in the special file system SHM (which is linked by the shmid_kernel structure).
The data that needs to be shared between processes is placed in a region called IPC shared memory, and all processes that need to access the shared zone map the shared area to the address space of the process. System V shared memory obtains or creates an IPC shared memory region through Shmget, and returns the appropriate identifier. The kernel guarantees that the shmget obtains or creates a shared memory area, initializes the corresponding Shmid_kernel structure of the shared memory area, and also creates and opens a file of the same name in the special file system SHM, and establishes the corresponding dentry and inode structure of the file in memory. The newly opened file does not belong to any one process (any process can access the shared memory area). All of this is done by the system call Shmget.
Note: Each shared memory area has a control structure struct Shmid_kernel,shmid_kernel is a very important data structure in the shared memory area, which is a bridge between storage management and file system, which is defined as follows:
struct Shmid_kernel/* Private to the kernel */{struct kern_ipc_permshm_perm;struct file *shm_file;intid;unsigned longshm _nattch;unsigned Longshm_segsz;time_tshm_atim;time_tshm_dtim;time_tshm_ctim;pid_tshm_cprid;pid_tshm_lprid;};

The most important domain in the structure should be shm_file, which stores the address of the file that will be mapped. Each shared memory area object corresponds to a file in the special file system SHM, in general, the file in the special file system SHM cannot be accessed using the read (), write () method, and when the shared memory is taken to map the file to the process address space, Access to the memory can be accessed directly using it. Here we use the chart in [1] to give a data-sharing structure related to System V shared memory: Like Message Queuing and semaphores, the kernel maintains all shared memory areas in the system through the data structure struct Ipc_ids shm_ids. The shm_ids.entries variable in the pointer points to an array of ipc_id structures, and each IPC_ID structure array has a pointer to the KERN_IPC_PERM structure. Readers should be familiar with this, for the System V shared memory area, the host of the kern_ipc_perm is the shmid_kernel structure, Shmid_kernel is used to describe a shared memory area, so that the kernel can control all the shared areas of the system. At the same time, the file type pointer shm_file in the SHMID_KERNEL structure points to the corresponding files in the file system SHM, so that the shared memory area corresponds to the file in the Shm file system.
After a shared memory region has been created, it is also mapped to the process address space, and the system calls Shmat () to complete this function. Because a file with the same name in the file system SHM has been created when you call Shmget (), the process of calling Shmat () is equivalent to a file procedure with the same name in the map file system SHM, similar to Mmap (). The System V Shared memory API has the following main Api:shmget (), Shmat (), SHMDT (), and Shmctl () for System V shared memory.
#include <sys/ipc.h> #include <sys/shm.h>int shmget (key_t key, size_t size, int flag); int shmctl (int shmid, in T-cmd, struct shmid_ds *buf), void *shmat (int shmid, const void *addr, int flag), int shmdt (void *addr);

Shmget () is used to obtain the ID of the shared memory area, and if the specified shared area does not exist, the corresponding zone is created. Shmat () maps the shared memory area to the address space of the calling process so that the process can easily access the shared area. The SHMDT () call is used to dismiss a process's mapping to a shared memory region. SHMCTL implements control operations on shared memory areas. Here we do not specifically introduce these system calls, readers can refer to the corresponding manual page, the following example will give their calling method.
Note: The internal implementation of Shmget contains many important system V shared memory mechanisms, and Shmat does not really change the page table of a process when it maps the shared memory area to process space. When a process accesses a memory-mapped region for the first time, it causes a page fault exception because there is no allocation of the physical page table, and the kernel then assigns the corresponding table of pages to the shared memory-mapped region based on the appropriate storage management mechanism. System V shared memory limit in the/proc/sys/kernel/directory, record the limit of System V shared memory, such as the maximum number of bytes in a shared memory area Shmmax, the system-wide maximum shared memory area identifier number Shmmni, etc., can be adjusted manually, but this is not recommended. System V Shared Memory Summary 1, The data in the System V shared memory is never written to the actual disk file, whereas shared memory traffic through MMAP () mapping ordinary files can specify when data is written to the disk file. Note: As mentioned earlier, the System V shared memory mechanism is actually implemented by mapping files in the special file system SHM, the installation point of the filesystem SHM on the swap partition, and after the system reboots, all the content is lost.
2. System V shared memory is persisted with the kernel, and even if all processes accessing shared memory have terminated gracefully, the shared memory area still exists (unless the shared memory is explicitly removed), and any overwrite operations on the shared memory area will persist until the kernel reboots.
3. When calling Mmap () to map ordinary files for interprocess communication, it is important to consider when the process terminates the impact on the communication. The process of communicating through System V shared memory is not. Reference http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html#ibm-pconhttp://www.ibm.com/ Developerworks/cn/linux/l-ipc/part5/index2.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.