Use MMAP for inter-process communication in Linux

Source: Internet
Author: User
At the operating system level, the virtual address space seen by each process is independent and there is no intersection between them. Therefore, you need to map different virtual addresses of multiple associated processes to the same physical address space through an intermediate Association. MMAP is such a function. it treats a file (also... information & nbs) as the virtual address space seen by each process is independent at the operating system level and there is no intersection between them. Therefore, you need to map different virtual addresses of multiple associated processes to the same physical address space through an intermediate Association.
 
MMAP is a function that maps a file (that is, the physical address space in the memory) to the address space of different processes.
 
I. write
 
It is worth noting that MMAP requires the physical address space in the memory. Therefore, during the write operation, you need to load the file into the memory for the operation. Therefore, you need to perform the write operation after opening the file, this causes a page missing exception and maps the file to the memory.
 

 
Fd = open (argv [1], O_CREAT | O_RDWR | O_TRUNC, 00777 );
Lseek (fd, 4, SEEK_SET); // here the size is best for the shared struct, and the entire size is loaded into the memory.
Write (fd, "", 1 );
P_mmap = (int *) mmap (NULL, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
Close (fd );
 
* (P_mmap) = 1;
If you do not perform the write null operation, mmap ING will be performed directly, resulting in a segment error because the physical memory corresponding to the actual fd does not exist.
 
 
II. Read
 
The read process must call mmap after the write process. Otherwise, a segment error occurs, which is the same as the previous one.
 
Read Process Flow
 
 
Fd = open (argv [1], O_CREAT | O_RDWR, 0077 );
P_mmap = (int *) mmap (NULL, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
Printf ("data: % d \ n", (* (p_mmap )));

From the bofusi computer column
Related Article

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.