Inter-process communication (III)-shared memory Zone

Source: Internet
Author: User

1. Overview

The shared memory zone is the fastest in IPC. When the memory zone is mapped to the address space of the process sharing it, data transmission between processes no longer involves the kernel.

But this requires some form of synchronization, the most common is the semaphore.

No kernel involved: processes no longer transmit data to each other by executing any system calls that enter the kernel. The kernel must establish a memory ing relationship that allows various processes to share the memory area, and then manage the memory area with a single value.

 

The problem with pipelines, FIFO, and message queues is that when two processes want to exchange information, the information must be transmitted through the kernel.

This problem can be bypassed in the shared memory area, but data must be synchronized.

 

Using the features of memory ing files, all I/O will no longer have the kernel to directly participate in Io. We will never call read, write, or lseek to simplify our code.

Not all files can perform memory ing. For example, access terminal or socket descriptor is not allowed. These types of descriptors must be accessed using read and write.

 

2. Related functions

MMAP maps files or POSIX shared memory area objects to the address space of the calling process.

Three goals:

(1) using common files, memory ing Io, open, and MMAP () methods is also suitable for unrelated processes.

(2) Special files, anonymous memory ing, MMAP (o_anon), open (/dev/zero), and MMAP () are used only for links with kinship.

(3) Use shm_open to provide a POSIX shared memory zone between unrelated processes. The shm_open and MMAP modes are suitable for unrelated processes.

# Include <sys/Mman. h>

Void * MMAP (void * ADDR, size_t Len, int Prot, int flags, int FD, off_t offset );

Success: Start address of the mapped area; error: map_failed

ADDR: Specifies the start address of the FD descriptor to be set to the in-process space. Generally, it is null and the start address is selected by the kernel.

Len: number of bytes mapped to the address space of the calling process. Count from offset.

Offset: usually 0. The offset of the memory mapped to the file.

Prot: the protection of the memory ing area, usually prot_read | prot_write

Flags: You can specify map_private: the modification of the mapped data by the calling process is only visible to the process.

Map_private: visible to all processes that share the data.

After MMAP is successful, FD can be closed and the ing has been established.

 

Output A ing from the process address space

# Include <sys/Mman. h>

Int munmap (void * ADDR, size_t Len );

 

Make the files on the hard disk consistent with the content in the memory ing area

Int msync (void * ADDR, size_t Len, int flags );

0;-1

Flags: ms_async: asynchronous write

Ms_sync: Synchronous write

Ms_invalidate: Make the cache invalid

3. Anonymous ing

If MMAP is called to provide a ing memory area that will be shared by the fork Parent and Child processes, that is, the memory is shared only between the kinship memories, you can use anonymous ing.

(1) 4.4bsd provides anonymous memory ing to avoid File Creation and opening.

Use flags to specify map_shared | map_anon, FD =-1, offset = 0. The initialization of such a memory partition is 0.

Int * IP = MMAP (null, sizeof (INT), prot_read | prot_write, map_shared | map_anon,-1, 0 );

(2) svr4 provides the/dev/Zero device file, which can be used in MMAP after being opened.

The Bytes returned when reading from the device are all 0, and any bytes written to the device are discarded. Many BSD also support this.

Int FD = open ("/dev/Zero", o_rdwr );

Int * IP = MMAP (null, sizeof (INT), prot_read | prot_write, map_shared, FD, 0 );

 

4. POSIX shared memory Zone

Open -- MMAP: POSIX memory ing file;

Shm_open -- MMAP: POSIX shared memory area object.

The two are called POSIX memory zone objects.

4.1. Open and delete

POSIX shared memory zone requires two steps:

(1) specify the name parameter to call shm_open to create a new shared memory area object or open an existing shared memory area object.

(2) Call MMAP to map the shared memory area to the address space of the process.

# Include <sys/Mman. h>

Int shm_open (const char * Name, int Oflag, mode_t mode );

Success: non-negative descriptor; error:-1;

Delete the name of a shared memory area object. It does not affect existing applications that support the underlying object until all applications of the object are closed.

Int shm_unlink (const char * Name );

 

Change the size of objects in common files or shared memory

# Include <unistd. h>

Int ftruncate (int fd, off_t length );

 

An existing shared memory area object or an object size

# Include <sys/type. h>

# Include <sys/STAT. h>

Int fstat (int fd, struct stat * BUF );

Struct stat {

Mode_t st_mode;

Uid_t st_uid;

Gid_t st_gid;

Off_t st_size; // size in bytes
};

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.