Linux interprocess communication--mmap shared memory (i)

Source: Internet
Author: User
Tags function prototype posix

Shared memory can be said to be the most useful inter-process communication and the fastest form of IPC. Two different processes A, B shared memory means that the same piece of physical memory is mapped to the respective process address space of process A and B. Process A can instantly see that process B updates the data in shared memory, and vice versa. Because multiple processes share the same block of memory, there is a need for some kind of synchronization mechanism, both mutexes and semaphores.

One obvious benefit of using shared memory communication is that it is efficient because the process can read and write directly to the memory without requiring any copy of the data. For communication methods such as pipelines and message queues, four copies of the data are required in the kernel and user space, while shared memory copies only two data [1]: One from the input file to the shared memory area, and the other from the shared memory area to the output file. In fact, when you share memory between processes, you do not always have to read and write small amounts of data, and then re-establish the shared memory area when there is new communication. Instead, the shared area is maintained until the communication is complete, so that the data content is kept in shared memory and is not written back to the file. Content in shared memory is often written back to a file when it is de-mapped. Therefore, the use of shared memory communication mode is very efficient.

The Linux 2.2.x kernel supports a variety of shared memory methods, such as mmap () system calls, POSIX shared memory, and System V shared memory. Linux distributions such as Redhat 8.0 support mmap () system calls and System V shared memory, but have not implemented POSIX shared memory, this article will mainly introduce the principle and application of mmap () system call and System V shared memory API.

2.mmap system Call

Mmap system Call Yes Yes the process is shared memory by mapping the same common file. After the normal file is mapped to the process address space, the process can access the file as if it were accessing normal memory, without having to call read again. Write, and so on. System calls that are used with mmap system calls are also Munmap,msync, and so on.

In fact, mmap system calls are not designed entirely for shared memory. It itself provides a different way of accessing ordinary files, which is that processes can operate on ordinary files like read-write memory. and POSIX or System V shared memory is purely for shared memory, Of course mmap implementation of shared memory is also one of the main applications.


#include <sys/mman.h>  void *mmap (void *start, size_t length, int prot, int flags, int fd, off_t offset);
The descriptions for each parameter are as follows:

  • Start: The starting address of the map area, set to 0, indicates the starting address of the mapping area determined by the system.
  • Length: Size of the map area. The length units are in memory pages.
  • Prot: Expected memory protection flag, cannot conflict with open mode of file. Is a value that can be logically grouped together by an OR operation.
    • Prot_exec//page content can be executed
    • Prot_read//page content can be read
    • Prot_write//page can be written
    • Prot_none//Page not accessible
  • Flags: Specifies the type of the mapped object, and whether the mapping options and mapping pages can be shared. Its value can be a combination of one or more of the following bits.
    • Map_fixed//Using the specified mapping start address, if the memory area specified by the start and Len parameters overlaps the existing mapping space, the overlapping portions will be discarded. If the specified start address is not available, the operation will fail. And the start address must fall on the boundary of the page.
    • Map_shared//share the mapping space with all other processes that map this object. Writes to the shared area are equivalent to outputting to a file. Until Msync () or Munmap () is called, the file is not actually updated.
    • Map_private//Create a private mapping of a write-time copy. The write to the memory area does not affect the original file. This flag is mutually exclusive with the above logo and can only be used.
    • Map_denywrite//This flag is ignored.
    • Map_executable//Ibid.
    • Map_noreserve//Do not reserve swap space for this mapping. When the swap space is preserved, changes to the map area may be guaranteed. When swap space is not preserved and memory is low, modifications to the map area can cause a segment violation signal.
    • map_locked//Locks the page of the map area, thus preventing the page from being swapped out of memory.
    • Map_growsdown//For the stack, telling the kernel VM system that the mapping area can be scaled down.
    • Map_anonymous//Anonymous mapping, the map area is not associated with any files.
    • Map_anon//map_anonymous's nickname, no longer used.
    • Map_file//compatible flag, ignored.
    • Map_32bit//The mapping area is ignored when the low 2gb,map_fixed of the process address space is specified. The current flag is only supported on the X86-64 platform.
    • Map_populate//For file Mappings prepare the page table in a pre-read manner. Subsequent access to the map area is not blocked by page violations.
    • Map_nonblock//is only meaningful when used with map_populate. Do not perform pre-reading, only the page table entry is established for pages that already exist in memory.
  • FD: Valid file descriptor. Typically returned by the open () function, whose value can also be set to-1, you need to specify the Map_anon in the flags parameter to indicate that anonymous mappings are in progress.
  • Offset: The starting point for the content of the mapped object. Typically set to 0, which represents the start of mapping from a file header.
The return value of the function is the address that the last file maps to the process space, and the process can manipulate the starting address directly to a valid address for that value. The system calls Mmap for shared memory in the following two common ways:
    • 1) Use the memory mapping provided by the normal file: applies to any process. At this point, you need to open or create a file, and then call mmap, this way there are many characteristics and to note the place, we are in the following or give an example of the explanation.
    • 2) using special files for anonymous memory mapping: Applies to relationships between processes. Because of the special affinity of the parent-child process, the mmap is used for the tag, and then the fork is called. Then after the fork is called, the child process rapidly inherits the address space of the parent process after the anonymous mapping. Also inherits the address returned by the mmap, so that the parent-child process can communicate through the mapping area. Note that the address returned by MMAP needs to be maintained by the parent process together.

The first approach can be used for any of the two processes, and the best way to implement shared memory for a relational process should be to use an anonymous memory map. At this point, you do not have to specify a specific file, as long as the corresponding flag can be set, followed by a corresponding example.

3.munmap system Call

The system call unlocks a mapping relationship in the process address space, and when the mapping relationship is lifted, access to the original mapped address causes a segment error to occur. Its function prototype is:

int Munmap (void *start, size_t length);  
where the parameter addr is the address returned when the mmap is called, Len is the size of the map area.4.msync system Call

In general, the process's changes to the shared content in the mapping space are not directly written back to the disk file, and are often performed after the call to Munmap. You can make the file content on disk consistent with the contents of the shared memory area by calling Msync. The prototype of the function is as follows:

int Msync (void * addr, size_t len, int flags);  
Addr: The address of the file mapped to the process space;
Len: The size of the mapping space;
Flags: Refreshed parameter settings, can be value ms_async/ms_sync/ms_invalidate
    • When the value is Ms_async (asynchronous), the call returns immediately, not equal to the completion of the update;
    • When the value is Ms_sync (synchronous), the call waits for the update to return after completion;
    • Fetch Ms_invalidate (notifies the process using the shared zone that the data has changed), after the shared content changes, the other mappings of the file are invalidated, allowing other processes that share the file to regain the latest values.
5. Application Examples

The instance contains two sub-programs, These two subroutines are compiled into Mmap_read and mmap_write. Two programs use command-line arguments to specify the pain of a file to implement inter-process communication in a shared memory manner. Mmap_write tries to open a normal file specified by the command line parameter, maps the file to the address space of the process, The mapped address space is then written. mmap_read map the file specified by the command line parameter to the address space of the process, and then perform a read operation on the mapped address space. So, Two processes use command-line arguments to specify the same file to implement interprocess communication in a shared memory manner. Write operation subroutine the source code is as follows:

/**************************************************************************************//* Introduction: System v Shared memory *//*** /#include <sys/ipc.h># Include <sys/shm.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include < stdlib.h> #include <string.h>typedef Struct{char name[4];int age;} people;int main (int argc, char** argv) {int Shm_id,i;people *p_map;char Name[4];shm_id=shmget (ipc_private,2048,0600); if (shm_id<0) {perror ("Shmget error"); return 1;} /* Write Operation */p_map= (people*) Shmat (shm_id,0,0), if (p_map<0) {perror ("Shmmat error"); return 1;} Name[0]= ' a '; name[1]= ' + '; for (i = 0;i<10;i++) {name[0]++;memcpy ((* (P_map+i)). Name,&name,sizeof (name));(* (p_ map+i)). Age=20+i;} if (SHMDT (p_map) <0) perror ("Detach Error");/* Read Operation */p_map = (people*) shmat (shm_id,null,0); for (i = 0;i<10;i++) { printf ("name:%s\n", (* (P_map+i)). Name);p rintf ("Age%d\n", (* (P_map+i)). age);} if (SHMDT (p_MAP) <0) perror ("Detach Error"); return 0;} 
in the above program, first defines a people data format (the shared memory area of the data often has a fixed format, by the various processes of communication, the use of the structure of the general representative). Mmap_write first Open or create a file, and set the length of the file to 5 people structure size. Then, starting with the return address of mmap, 10 people structures are set up. Then the process sleeps 10s, waits for other processes to map the same file, and finally de-maps. The source code for the read operation subroutine is as follows:

/************ * Introduction: Mmap_read shared memory, read operation Sub-program *//******* /#include <sys/mman.h># Include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include < Stdlib.h>typedef Struct{char name[4];int age;} People;int Main (int argc, char** argv) {int fd,i;people *p_map;if (argc! = 2) {perror ("Usage:mmap_read <mmap file>"); return 1;} Fd=open (argv[1],o_creat| o_rdwr,00777);p _map = (people*) mmap (null,sizeof (people) *10,prot_read| prot_write,map_shared,fd,0); close (FD); for (i = 0;i<10;i++) {printf ("Name:%s-Age%d;\n", (* (P_map+i)). Name, (* (P_map +i)). age);} Munmap (p_map,sizeof (people) *10); return 0;} 
Mmap_read simply maps a file and reads 10 people structures from the address returned by the mmap in the format of the people data structure, outputs the read value, and then unlocks the map. Is the result of the operation.

after the file is mapped, the process that calls Mmap to the return address is actually access to a memory area, temporarily out of the file on disk. All operations on MMAP return address space are only meaningful in memory, The corresponding content in memory is written back to the disk file only if Munmap or or msync is called.

Linux interprocess communication--mmap shared memory (i)

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.