Mmap of Linux shared memory

Source: Internet
Author: User

This should be considered to be an IPC, although efficiency may not be as high as other IPC methods.

It's easy to see maps that map. Indeed, Mmap is a mapping that maps open files to a contiguous set of memory. The memory can be manipulated to read and write to the file, in turn, that is, it is possible to achieve process communication in this way.

The MMAP series involves three functions.

void * Mmap (void *buf, size_t len, int prot, int flag, int fd, off_t offset);

This function establishes a shared memory, PROT is the permission, the optional value is Prot_read, Prot_write, Prot_exec, Prot_none. As the name implies, not much to say.

Flag is the shared memory scope. Some of the desirable values are map_shared (process sharing, that is, when a modification is made, it is written back to the file), Map_private (private, which does not affect the file), map_fixed (in general, the BUF parameter is set to NULL, The address is assigned by the system, but if this field is specified, buf cannot be empty, otherwise core dump).

int Msync (void *buf, size_t len, int flags);

This function synchronizes shared memory with the value of the file, flags should have the following values, Ms_async (synchronous), Ms_sync (asynchronous), Ms_invalidate (read back data from the file).

Munmap (void* buf, size_t len);

This function turns off shared memory.


Use these common options to write a simple applet.

#include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <sys/mman.h> #include    <fcntl.h>typedef struct {int integer; Char string[24];}    Record; #define Nrecord 5int Main (int argc, char * * argv) {record *mapped;        int I, F;    f = open ("Record.dat", O_RDWR); Mapped = (record*) mmap (0, sizeof (RECORD) * Nrecord, Prot_read |    Prot_write, map_shared, F, 0);    printf ("********************first of all***********************\n"); for (i = 0; i < Nrecord; i++) {printf ("record (%d) is%s\n", (mapped + i)->integer, (mapped + i)->stri        NG);        (mapped + i)->integer + = 100;    snprintf ((mapped + i)->string, "record-%d", (mapped + i)->integer);   } msync (void *) mapped, sizeof (RECORD) * Nrecord, ms_invalidate);    Ms_invalidate Ms_async Ms_sync These three macros can achieve the effect of updating the file. I don't know the difference between the three.    printf ("********************second of all***********************\n"); for (i = 0; i < Nrecord; i++) {printf ("ReCord (%d) is%s\n ", (mapped + i)->integer, (mapped + i)->string);        (mapped + i)->integer + = 100;    snprintf ((mapped + i)->string, "record-%d", (mapped + i)->integer);    } msync ((void*) mapped, sizeof (RECORD) * Nrecord, Ms_async);    printf ("********************third of all***********************\n"); for (i = 0; i < Nrecord; i++) {printf ("record (%d) is%s\n", (mapped + i)->integer, (mapped + i)->stri        NG);        (mapped + i)->integer + = 100;    snprintf ((mapped + i)->string, "record-%d", (mapped + i)->integer);    } munmap ((void*) mapped, sizeof (RECORD) * Nrecord); return 0;}

These three functions in the operation of the file, in fact, the advantages are very obvious. But when it comes to sharing memory, I believe this is definitely not a good choice.

Mmap of Linux shared memory

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.