Linux interprocess communication--shmget () shared Memory (ii)

Source: Internet
Author: User
Tags int size

A shared memory area is a portion of physical memory that is shared by multiple processes. If more than one process maps the region of memory to its own virtual address space, those processes can access the shared memory area directly, allowing communication through the zone. Shared memory is the quickest way to share data between processes, where a process writes data to a shared memory region, and all processes that share that memory area can immediately see the contents. This page of shared virtual memory appears in the page table of each process that shares the page. However, it does not need to have the same virtual address in the virtual memory of all processes.


Graph Shared Memory Mapping diagram

Like all System V IPC objects, the acquisition of a shared memory object is controlled by key. After the memory is shared, no checks are made on how the process uses the memory. They must rely on other mechanisms, such as System V beacons, to synchronize access to the Shared memory area (how the semaphore Controls access to the critical code to another speech).

Each newly created shared memory object is expressed in a Shmid_kernel data structure. All SHMID_KERNEL data structures in the system are stored in the Shm_segs vector table, and each element of the vector table is a pointer to the SHMID_KERNEL data structure.
The Shm_segs vector table is defined as follows:
struct Shmid_kernel *shm_segs[shmmni];

A Shmmni of 128 indicates that there can be a maximum of 128 shared memory objects in the system.

The data structure Shmid_kernel is defined as follows:
struct Shmid_kernel
{
struct Shmid_ds u; /* The following is private * *
unsigned long shm_npages; /* Size of segment (pages) */
unsigned long *shm_pages; /* Array of Ptrs to frames-SHMMAX */
struct Vm_area_struct *attaches; /* Descriptors for attaches */
};

which
Shm_pages represents an array of memory pages occupied by the shared memory object, and each element of the array is, of course, the starting address of each memory page.
Shm_npages is the number of memory pages that the shared memory object occupies, in pages. This amount of course covers the smallest integer multiples of the application space.
(A new Shared memory segment, with size equal to the value of size rounded up to A multiple of page_size)
Shmid_ds is a data structure that describes the authentication information for this shared memory area, the size of the bytes, the last glue time, the separation time, the change time, the process that created the shared area, the last process to manipulate it, and the current number of processes that are using the information.
It is defined as follows:
struct Shmid_ds {
struct Ipc_perm shm_perm; /* Operation Perms */
int shm_segsz; /* Size of segment (bytes) */
__kernel_time_t Shm_atime; /* Last Attach time */
__kernel_time_t Shm_dtime; /* Last Detach time */
__kernel_time_t Shm_ctime; /* Last Change time */
__kernel_ipc_pid_t Shm_cpid; /* PID of Creator */
__kernel_ipc_pid_t Shm_lpid; /* PID of last operator */
unsigned short shm_nattch; /* No. of current attaches */
unsigned short shm_unused; /* Compatibility */
void *shm_unused2; /* ditto-used by DIPC */
void *shm_unused3; /* Unused */
};

Attaches describes the virtual memory regions of each process that are mapped by the shared physical memory object. Every process that wants to share this memory must be associated (attach) to its virtual memory through a system call. This process creates a new VM_AREA_STRUCT data structure that describes the shared memory for the process. You can specify the location of the virtual address space within the share if it is created, or you can let Linux choose a sufficient free area for it.

This new vm_area_struct structure is the relationship between maintaining shared memory and the processes that use it, so in addition to associating process information, indicate where the shared memory data structure Shmid_kernel is located; In addition, it is easy to manage these frequently changing vm_area_struct, so the data structure is organized in a chain form, and the list is directed by attaches, while the VM_AREA_STRUCT data structure provides two pointers specifically: Vm_next_shared and vm_ Prev_shared, which is used to connect the VM_AREA_STRUCT data structure of the shared area in each process in which it is used.

Figure system V IPC mechanism-shared memory

Linux provides four kinds of operations for shared memory.
1. Creation or acquisition of shared memory objects. Like the other two IPC mechanisms, a process must create a shared memory object with a key value of keys by calling SYS_IPC (call value Shmget) before using a shared memory region, or obtain a reference identifier for a shared memory object that already exists with key value key. Subsequent access to the shared memory object is done through the reference identifier. The creation or acquisition of a shared memory object is done by the function sys_shmget, which is defined as follows:
int Sys_shmget (key_t key, int size, int shmflg)

Here key is the key value representing the shared memory object, size is the amount of the Shared memory area (in bytes), and SHMFLG is the flag (the special requirement for the shared memory object).

The work it does is as follows:
1) If key = = Ipc_private, a new shared memory object is always created.
But (the name choice Ipc_private was perhaps unfortunate, ipc_new would more clearly show its function)
* Calculate the number of pages to use for size and check the legality.
* Apply a piece of memory to build the Shmid_kernel data structure, note that the size of the requested memory area does not include the true shared memory area, and actually the shared memory area is not really created until the first process tries to access it.
* Based on the number of pages occupied by the shared memory area, request a space for the page table (4 bytes per page) and clear the page 0.
* Search the vector table Shm_segs to find an empty location for the newly created shared memory object.
* Fill in the Shmid_kernel data structure and add it to the empty position found in the vector table Shm_segs.
* Returns the reference identifier of the shared memory object.

2) in the vector table Shm_segs, look for a shared memory object with the key value, and the result is three:
* If it is not found and is not indicated in the action flag SHMFLG to create a new shared memory, the error is returned, otherwise a new shared memory object is created.
* If it is found, but the operation requires that a new object with key value be created, then the error is returned.
* Otherwise, the legality, authentication check, if wrong, the error is returned; otherwise, the reference identifier of the memory object is returned.

The creator of a shared memory object can control access to the memory and whether its key is public or private. If sufficient permissions are available, it can also lock the shared memory in physical memory.
See include/linux/shm.h

2. Association. After you create or obtain a reference identifier for a shared memory region, you must also map (glue) The shared memory area to the virtual address space of the process before you can use the shared memory region. The system calls SYS_IPC (call value Shmat) to share the mapping of the memory area to the process virtual address space, and the function Sys_shmat that really completes the glue action,

It is defined as follows:

#include <sys/types.h>
#include <sys/shm.h>

void *shmat (int shmid, const void *shmaddr, int shmflg);



which
Shmid is the reference identifier of the shared memory object returned by Shmget;
The shmaddr is used to specify the virtual address of the shared memory area corresponding to the virtual address space of the process;
SHMFLG is a mapping flag;
The virtual address that is returned in the process

The function does the following work:
1) Locate the shared memory object according to Shmid.
2) If SHMADDR is 0, that is, the user does not specify the location of the shared memory area in its virtual space, the system will find an area for it in the virtual address space of the process (starting at 1G); otherwise, use SHMADDR as the virtual address of the map.
(If shmaddr is NULL, the system chooses a suitable (unused) address a he which to attach the segment)
3) Check the legitimacy of the virtual address (cannot exceed the maximum virtual space size of the process -3g, not too close to the top of the stack stack).
4) certification check.
5) apply for a piece of memory for building data structure vm_area_struct, fill in the structure.
6) Check the memory area and add it to the process's mm structure and the vm_area_struct queue of the shared memory object.

The glue for shared memory simply creates a vm_area_struct data structure and joins it to the appropriate queue, and does not create a true shared memory page at this time.

When a process accesses a page of shared virtual memory for the first time, a page fault exception occurs because all the shared memory pages are not allocated. When Linux processes this page fault, it finds the VM_AREA_STRUCT data structure of the virtual address where the exception occurred. The data structure contains a set of handlers for such shared virtual memory, where the nopage action is used to handle situations where the physical page corresponding to the virtual page does not exist. For shared memory, the operation is shm_nopage (defined in IPC/SHM.C). This operation looks up the page table entry for the virtual address of the page fault exception that describes the Shmid_kernel data structure of this shared memory, to see if the shared page exists (the page table entry is 0, which indicates that the shared page was used for the first time). If it does not exist, it allocates a physical page and creates a page table entry for it. This entry not only goes into the page table of the current process, but also to the page table Shm_pages of the SHMID_KERNEL data structure.

When the next process tries to access this memory and gets a page fault, the same path goes through the function shm_nopage. At this point, when the function looks at the page table Shm_pages of the SHMID_KERNEL data structure, it discovers that the shared page already exists, and it simply fills in the page table entry to the corresponding location in the Process page table, without having to recreate the physical page. Therefore, the first process to access a shared memory page is created, and the other processes that subsequently access it add this page only to their virtual address space.

3. Detach. When the process no longer needs to share virtual memory, they are separated from it (detach). As long as other processes are still using this memory, this separation will only affect the current process, without affecting other processes. The VM_AREA_STRUCT data structure of the current process is removed from the Shmid_ds and released. The page table of the current process is also updated, and the virtual memory pages corresponding to the shared memory are marked as invalid. When the last process to share this memory is detached from it, the shared memory page is freed, and the Shmid_kernel data structure of the shared memory is freed.

The system calls SYS_IPC (call value SHMDT) is used to separate the shared memory area from the process virtual address space, and the function that actually completes the detach action

SYS_SHMDT, which is defined as follows:
int Sys_shmdt (char *shmaddr)

Where Shmaddr is the start virtual address of the shared page to be detached by the process.

The function searches the memory structure of the process for all VM_AREA_STRUCT data structures, finds the address shmaddr corresponding to one, calls the function Do_munmap releases it.

In the function Do_munmap, the VM_AREA_STRUCT data structure to be freed is removed from the virtual memory of the process, clearing its corresponding page table entry in the Process page table (which may account for more than one page table entry).

If the shared virtual memory is not locked in physical memory, the separation is more complex. Because in this case, the shared memory page may be swapped to the system's swap disk when the system is using memory heavily. In order to avoid this situation, you can lock a shared memory page in physical memory by using the following control operation to not allow out-of-exchange. The swap and swap of shared memory are discussed in the 3rd chapter.

4. Control. The fourth operation that Linux implements on shared memory is the control of shared memory (call value is Shmctl's SYS_IPC invocation), which is implemented by the function Sys_shmctl. The control operation includes acquiring the state of the shared memory object, setting the parameters of the shared memory object (such as UID, GID, mode, CTime, etc.), locking and releasing the shared memory object in memory (adding or removing the shm_locked flag on the object's mode), freeing the shared memory object resource, and so on.

Shared memory provides a fast and flexible mechanism that allows processes to share large amounts of data directly, without using copies or system calls. The main limitation of shared memory is that it cannot provide synchronization, if two processes attempt to modify the same shared memory region, because the kernel cannot serialize these actions, the written data may be arbitrarily mixed with each other. So processes that use shared memory must design their own synchronization protocols, such as with semaphores.

The following are the basic operations for interprocess communication using the shared memory mechanism:

header files that need to be included:

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

1. Create Shared Memory:

int Shmget (key_t key,int size,int SHMFLG);

Parameter description:

Key: A keyword used to represent a new or existing shared memory.

Size: The amount of shared memory created.

SHMFLG: A special flag that can be specified. IPC_CREATE,IPC_EXCL and low nine-bit permissions.

eg

int shmid;

Shmid=shmget (ipc_private,4096,ipc_create| ipc_excl|0660);

if (shmid==-1)

Perror ("Shmget ()");

2. Connect Shared memory

char *shmat (int shmid,char *shmaddr,int SHMFLG);

Parameter description

Shmid: Keywords for shared memory

SHMADDR: Specifies where the shared memory appears in the process memory address, usually we let the kernel decide for itself a suitable address location, using the time set to 0.

SHMFLG: Make a special mark.

eg

int shmid;

Char *SHMP;

Shmp=shmat (shmid,0,0);

if (shmp== (char *) (-1))

Perror ("Shmat () \ n");

3. Using Shared memory

When using shared memory, it is important to note that in order to prevent memory access violations, we typically use a combination of semaphores.

4. Separating shared Memory: When the program no longer needs to be shared inside, we need to separate the shared memory so that it can be freed, the function of separating shared memory is as follows:

int Shmdt (char *shmaddr);


5. Releasing shared memory

int shmctl (int shmid,int cmd,struct shmid_ds *buf);

Four: summary

System V shared memory is organized in the form of files in the special file system SHM. The identifier for shared memory can be created or obtained through Shmget. once the shared memory identifier is obtained, the memory area is mapped to the virtual address space of the process via Shmat (this process is mapped by opening up a memory).

1, System V shared in-memory data, never write to the actual disk file, and through the mmap () mapping ordinary file implementation
Shared memory traffic can specify when data is written to the disk file. Note: As mentioned earlier, the System V shared memory mechanism is actually
Over-mapped files in the special file system SHM implemented, file system SHM mount point on the swap partition, after the system reboots,
All the content is lost.
2, the System V shared memory is continuous with the kernel, even if all access to shared memory has been terminated gracefully, the shared memory area is still
(unless the shared memory is explicitly removed), any overwrite of the shared memory area will occur before the kernel reboots.
Kept.
3. When calling Mmap () to map ordinary files for interprocess communication, it is important to consider when the process terminates the traffic
Ring. The process of communicating through System V shared memory is not. Note: There are no examples of the use of shmctl, principles and
The same as the rest queue.

Shared memory allows two or more processes to share a given store because the data does not need to replicate back and forth, so it is the fastest
Inter-process communication mechanism. Shared memory can be mapped by mmap () to ordinary files (and, in special cases, anonymous mappings) mechanisms
Implementation can also be achieved through the System V shared memory mechanism. The application interface and principle are simple and the internal mechanism is complex. In order to achieve more security
All communication, often also with the signal and other synchronization mechanism of the common use.


Linux interprocess communication--shmget () shared Memory (ii)

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.