IPC _ Memory Sharing

Source: Internet
Author: User

Memory Sharing notes:
1. It allows two unrelated processes to access the same logical memory. (The memory is shared)
2. It does not provide a synchronization mechanism, so we need to use other synchronization methods to achieve the synchronization effect of memory access. (Synchronization is generally achieved by passing small messages .)
3. Functions of shared memory are similar to semaphores (Functions of the IPC Mechanism are similar)
Void * shmat (INT shm_id, const void * shm_addr, int shmflg );
Int shmctl (INT shm_id, int cmd, struct shmid_ds * BUF );
Int shmdt (const void * shm_addr );
Int shmget (key_t key, size_t size, int shmflg); # include <sys/SHM. h>
4. Create shared memory:
Shmget: Key, size byte is the size of the specified shared memory, ipc_creat and a permission flag | operation to create a new shared memory segment. The memory read/write permission is given for the permission flag. A non-negative integer is returned for success, and-1 is returned for failure.
Shmat: Once created, it cannot be shared by the process. We must map it to the process memory space of our process. This method is the function of the bird.
* Shm_addr: Generally, a null pointer indicates that the system selects a process address as the ing address of the shared memory.
Shmflg: Usually we give 0, there are two ways to give: shm_rnd (related to shm_addr) and shm_rdonly read-only: If the created access permission is writable, but rdonly is set here, so it is also rdonly.
The first address is returned successfully, and-1 is returned if the request fails.
Shmdt: un ing: separates the shared memory from the current process, so that the process cannot use Memory Sharing. 0 is returned for success,-1 is returned for failure.
Shmctl: Command: ipc_stat (as if it is the same as ipc_set) ipc_set value ipc_rmid is deleted (in this case, struct Buf is given to 0)
* Buf: indicates the structure that includes the shared memory mode and access permissions.
Success 0, failed-1.s
NOTE: If shmctl deletes a linked shared memory, the shared memory segment can still be used until it is detached from the last process. (But this is not standardized, so do not rely too much on this concept)

In actual programming, we usually use semaphores, Transfer Messages (pipelines or IPC message queues), generate signals, and other methods to Achieve Synchronous access to the shared memory.

The following is a transfer (Supplement ):

1. Data Structure

Similar to the message queue and semaphore set, the kernel maintains a special data structure shmid_ds for each shared memory segment (which exists in its address space), which is in include/Linux/SHM. H is defined as follows:

/* Each shared memory segment in the system has a shmid_ds data structure .*/

Struct shmid_ds {

Struct ipc_perm shm_perm;/* operation permission */

Int shm_segsz;/* segment size (in bytes )*/

Time_t shm_atime;/* time when the last process was appended to this segment */

Time_t shm_dtime;/* time when the last process leaves the segment */

Time_t shm_ctime;/* the last time this structure was modified */

Unsigned short shm_cpid;/* Create the PID of this process */

Unsigned short shm_lpid;/* PID of the last process operated on this segment */

Short shm_nattch;/* Number of processes currently appended to this segment */

/* The following is private */

Unsigned short shm_npages;/* segment size (in page )*/

Unsigned long * shm_pages;/* pointer array pointing to frames-> shmmax */

Struct vm_area_struct * attaches;/* description of shared segments */

};

------

2. Shared Memory Processing Process

When a process accesses the shared virtual memory for the first time, a page error occurs. In this case, Linux finds out the vm_area_struct structure describing the memory, which contains the processing function address used to process this shared virtual memory segment. The error handling code for missing pages in the shared memory searches the table items in the shmid_ds page to check whether the table items in the shared virtual memory exist. If no, the system allocates a physical page and creates a page table item. This page table item is added to the shmid_ds structure and also to the page table of the process. This means that a page missing exception occurs when the next process attempts to access the page memory. The page missing Exception Handling Code of the shared memory gives the newly created physical page to the process. (Therefore, the first process's access to the shared memory results in the creation of a new physical page, and the access of other processes to the shared memory causes that page to be added to their address space .)

When a process no longer shares its virtual memory, it uses system calls to remove the shared segment from its virtual address area and update the process page table. After the last process releases the shared segment, the system releases it to the physical page allocated to the shared segment.

When the shared virtual memory is not locked to the physical memory, the shared memory may be switched to the swap zone.

--------

System Call: shmctl ()

Prototype: int shmctl (INT shmqid, int cmd, struct shmid_ds * BUF );

Return Value: 0 for success and-1 for failure

This special call is almost the same as semctl () call. Therefore, we will not discuss it in detail here. Valid command value:

Ipc_stat: retrieves the shmid_ds structure of a shared segment and stores it in the address of the Buf parameter.

Ipc_set: for a shared segment, set the value of the ipc_perm field of the shmid_ds structure from the Buf parameter.

Ipc_rmid: Mark a field as deleted

The ipc_rmid command does not delete a segment from the kernel, but only marks the segment as deleted. The actual deletion occurs when the last process leaves the shared segment.

When a process no longer needs to share the memory segment, it will call the shmdt () system call to cancel the segment. However, this does not actually Delete the segment from the kernel, instead, the shm_nattch Domain value of the shmid_ds structure is reduced by 1. When this value is 0, the kernel physically deletes the shared segment.

Memory sharing is the fastest IPC Mechanism, Because reading its access does not need to involve the kernel. So fast. But we also need to provide a synchronization mechanism for it.

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.