interprocess communication (Shared memory map area)

Source: Internet
Author: User

Function: Mmap ()

Function: Creates a memory map area, maps the file data on disk to memory, and the user can modify the disk file through memory.

Advantages: High Efficiency

Cons: Do not block, trouble (for efficiency is not very high demand advice with piping)

Function Prototypes:

#include <sys/mman.h>void *mmap (voidint int int FD, off_t offset); int munmap (void *addr, size_t length);

Mmap parameter Description:

    • Addr: The first address of the memory-mapped area, passed null
    • Length: Map area size, can not be 0, the general length of the file to specify how large (can be obtained through Lseek)
    • Prot: Map Area permissions (Prot_read, prot_write) must have Read permissions
    • Flags: Set sharing or Private (map_shared, map_private)
    • FD: File descriptor, open file requires Read permission
    • Offset: Mapping file offsets, when mapping the file pointer offset (must be an integer multiple of 4k) general fill 0

Return value: Successfully returned the first address of the file memory map area, failed to return map_failed = = (void *) (-1)

Munmap parameter Description:

    • Addr:mmap the first address of the memory-mapped area returned
    • Length: Map area size

Return value: Successfully returned 0, failed to return-1

Inter-process communication:

Blood-related interprocess communication: parent-child process shared memory map area (can create anonymous memory map area)

Unrelated interprocess communication: cannot communicate through anonymous memory mapping, only with disk file communication

The mmap is non-blocking.

If you create an anonymous memory map area:

    • Length needs to specify memory map area size
    • The flags parameter needs to add Map_anon macros
    • FD parameter should be changed to-1

Problems:

    1. If the return value (PTR) is the + + operation (ptr++), will munmap succeed?
      No, release the memory without changing it.
    2. If Open is O_rdonly, Mmap Pro? Parameter Designation Prot_read | What will happen to Prot_write?
      Mmap call failed, open is permission must be greater than or equal to mmap when the memory-mapped area
    3. What happens if the file offset is 1000?
      The call fails, and the offset must be an integer multiple of 4k
    4. Mmap What happens when the call fails?
      Question 2, 3. And then there is length = = 0 O'Clock
    5. Can I o_creat a new file to create a map area when open?
      Yes, but the size can not be 0, need to do file expansion
    6. What happens if a file is written over the memory-mapped area beyond the size of the memory map?
      The program does not go wrong, but the portion of the memory-mapped area is not written to the file

Search

Copy

interprocess communication (Shared memory map area)

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.