Inter-process communication (IPC) shared memory

Source: Internet
Author: User
Tags semaphore

★IPC methods include pipes, Message Queuing (message_queue), Semaphore, shared Memory (Sharememory), and sockets

(Socket). Inter-process communication mainly includes pipelines, system IPC (including the interest-bearing queue, signal, and shared storage), socket (socket).

This article will describe the content of shared memory.


Before you do this, take a look at the contents of the memory image of the process.

One, the memory image of the process (as distinct from the executable program file):

Refers to how the kernel stores executable program files in memory. The difference between the two is shown in three ways: the ① executable is located on the hard disk, and the memory image bit

② the executable program does not have a stack, because the stack is allocated only when the program is loaded into memory, and the ③ executable is static because it

is not running yet, but the memory image is dynamic and the data is changed as the operation progresses.

The memory image of the C program Generation process under Linux consists of four steps: precompiled, compiled, compiled, and linked. Compiler GCC is precompiled, compiled, compiled

3 steps to convert the source program files to a destination file. If a program has multiple target files or library functions are used in the program, the compiler also needs to have all the target files and the required

The library file is linked together and the executable program is generated. When the program executes, the operating system copies the executable program into memory . Its layout is as follows:






Note: The area where the body is located is a read-only code and a read-only data region, which is commonly referred to as the code snippet that holds the program's execution code, including

read-only constant variables, such as: string constants, and so on. The uninitialized data region, called the BSS (Block Started by Symbol) segment, is used to store

The uninitialized global variable, which is part of the static memory allocation. The initialized data region is called the data segment and is used to hold the initialized global variable

is also part of the static memory allocation, it is generally customary to refer to BSS segments and data segments collectively as data segments.







Ii. address assignment when different processes read the same data block






Note: The address space in the figure is the virtual space address, you can see the different processes PCB1 and PCB2 read the same address space, through the page table and also table

The descriptor MMU points to a different physical address, so even though the surface It looks like two processes access the same address, and its actual mapping gets to the physical

The address is not so.





Three, shared memory

shared memory is refers to some of the physical memory that is shared by multiple processes. A process to a shared memory region once the data is written, the memory area is shared

can see the content directly in the process . shared memory allows two unrelated processes to access the same logical memory , which is two running

A very efficient way to share and pass data between processes. Processes can connect the same piece of shared memory to their own address space, and all processes

are available to access the addresses in the shared memory, This means that once the process changes the data in the shared memory, it affects other processes that access the zone.



★ A statement about shared memory functions, in the header file sys/shm.h.






Note: The Red box section of the figure shows the generic relationship between shared memory (SHM) and interprocess communication, as well as the structure of the IPC Perm. The White box indicates a total

Four commonly used operating functions for memory sharing.




1.shmat function:

When a shared memory is created for the first time, it cannot be accessed by any process, and the Shmat function is used to initiate access to the shared memory and to

memory connection to the address space of the current process. The prototypes are as follows :




Note: You can see that there are three shapes in the function shm_id is the shared memory identity returned by the Shmget function; shm_addr specifies a shared memory connection

to the address location in the current process, which is generally empty, indicating that the system can select a total The SHM_FLG is a set of flag bits, typically 0. Their return

A return value call returns a pointer to the first byte of shared memory when successful, and returns 1 if the call fails.





2.shmctl function:

As with the Semctl function of semaphores, it is used to control shared memory, The prototype is shown below :



Note: The SHMCTL function also has three parameters, shm_id is the shared memory identifier returned by the Shmget function;

Three values: ①ipc_staT: Sets the data in the SHMID_DS structure The current associated value for shared memory, which is the current off of shared memory

②ipc_set: If the process has sufficient permissions, the current association value of the shared memory is set to the SHMID_DS structure in the Shmid_ds value;

The value given; ③ipc_rmid: Deletes a shared memory segment. BUF is a struct pointer that points to the structure of shared memory mode and access rights.





3.SHMDT function:

used to detach shared memory from the current process. (note: separating shared memory is not removing it, just making that shared memory no longer for the current process

available ). The prototype is shown in:




Note: The parameter shmaddr is the address pointer returned by the SHMAT function, returns 0 when the return value call succeeds, and returns-1 on failure.





4.shmget function:

For creating shared memory, its function prototypes are as follows:




Note: The function has three parameters, key and semaphore Semget function, the program needs to provide a parameter key (not 0 integer), effectively for the total

The shmget function returns a share associated with a key when it succeeds Memory Identifier (non-negative integer) for subsequent shared memory letters

Number, the call fails returns -1;size specifies the amount of memory that needs to be shared in bytes; SHMFLG is a permission flag that functions with the Open function

Mode parameter, you can do "or" with ipc_creat if you want to create it if the shared memory identified by the key does not exist. Share inside

The permission flags that are stored are the same as the file read and write permissions.



▲ irrelevant processes can be access the same shared memory through the return value of the function, which represents a resource that the program may want to use for all shares within

access is indirect, the program first calls the Shmget function and provides a key that is then generated by the system with a corresponding shared memory identifier

(The return value of the Shmget function), only the Shmget function uses the semaphore key directly, and all other semaphore functions are returned using the Semget function

identifier of the semaphore.




★ Exercise: Create two processes, create a shared memory in a process, write data to it, and read the data from shared memory through the B process.


1.shm_comm.h file:




2.READ_PROCESS.C file:




3.WRITE_PROCESS.C file:






4Makefile Automated Compilation files:





★ Running Result:






★ Summary:

Shared Memory Features:

① is the fastest in interprocess communication and needs to be suspended . Cause: The contents of the file written in the shared memory Area can be directly visited by both the shared Q, there is no data copy of the time consumed, so the speed is fast.

② does not provide any synchronization and mutual exclusion mechanism, the user should be self-maintenance (through the signal volume and set the synchronization mechanism, etc.).

The ③ interface is relatively simple.







Inter-process communication (IPC) 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.