Inter-process communication---shared memory

Source: Internet
Author: User
Tags function prototype

    1. What is shared memory?

      shared memory allows two unrelated processes to access the same piece of physical memory . Processes can connect the same piece of shared memory to their own address space, and all processes can access addresses in shared memory. If a process writes data to shared memory, the changes will immediately affect other processes that can access the same piece of shared memory.


    2. Function interface

      (1) Create shared memory

Function prototype: int shmget(key_t key,size_t size,int SHMFLG)

Key: Identifier returned by the Ftok () function

Size: The amount of memory that needs to be shared in bytes

SHMFLG:

Ipc_creat: Used alone, if shared memory does not exist, create, otherwise, open directly.

IPC_EXCL: No meaning for use alone

Ipc_creat | IPC_EXCL: If not present, create, otherwise return -1.


(2) access to shared memory

Function prototypes: void *shmat(int shm_id,const void* shm_addr,int shmflg)

SHM_ID: Is the identifier returned by the Shmget function

SHM_ADDR: Specifies the address location of the shared memory connection to the current process, usually null, indicating that the system chooses the address of the shared memory

Shm_flg:ipc_creat or 0

return value: A pointer to shared memory was returned on success, and the failure returned -1.


(3) separating shared memory from the current process

function prototypes: int shmdt(const void* SHMADDR)

The return value of the Shmaddr:shmat function

Return value: Successfully returned 0, failure returned-1.


(4) controlling memory sharing

function prototypes: int shmctl(int shm_id,int cmd,struct shmid_ds* buf)

The identifier returned by the Shm_id:shmget function

Cmd:

Ipc_stat: Sets the data in the SHMID_DS structure to the current associated value of shared memory, which overwrites the value of Shmid_ds with the current associated value of shared memory.

Ipc_set: If the process has sufficient permissions, set the current association value of shared memory to the value given in the SHMID_DS structure

IPC_RMID: Deleting shared memory segments

"Shm.h"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/84/8C/wKiom1eULx3gvoAvAAA_namximQ768.png "style=" float: none; "title=" 2.PNG "alt=" Wkiom1eulx3gvoavaaa_namximq768.png "/>

"SHM.C"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/8C/wKioL1eULx2hpMWYAACNMqwD43c488.png "style=" float: none; "title=" 3.PNG "alt=" Wkiol1eulx2hpmwyaacnmqwd43c488.png "/>

"TEST.c"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/8C/wKiom1eULx7wiORDAABq_LgH8lE530.png "style=" float: none; "title=" 5.PNG "alt=" Wkiom1eulx7wiordaabq_lgh8le530.png "/>

Operation Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/8C/wKioL1eULx6QAiVmAAA3tIJPfwY905.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1eulx6qaivmaaa3tijpfwy905.png "/>


Results Analysis: The case of the root shared memory is printed every second.

First, the parent process creates the shared memory, the parent process creates the child process, both parent-child processes are associated with shared memory, the number of associations is changed from 0 to 2, because the parent process has a short rest time, the association is canceled, the number of associations becomes 1, the last child process is also de-correlated, the number of associations becomes 0, and


3.mmap function

Mmap maps a file or other object into memory. Mmap can also implement shared memory. mmap function calls enable shared memory between processes by mapping the same file. After the file is mapped to the process address space, the process can manipulate the file like read-write memory.

Function prototype: void* mmap(void* addr,size_t length,int prot,int flags,int fd,off_t offset);


Addr: The start address of the map area, set to 0, indicates the system determines the starting address of the map area

Length: The size of the map area. length units are bytes

Prot: The desired memory protection flag. Take the following values:

Port_exec: page content can be executed Port_read: page content can be read

Port_write: page content can be written to Prot_none: page content is inaccessible

Flag: Specifies the type of the mapped object, and whether the mapping options are shared with the mapped page.

map_shared: shares the mapping space with all other processes that map this object.

Map_private: establishes a private mapping of a write-time copy. The write to the memory area does not affect the original file.

              map_fixed: Using the specified mapping start address, if the memory area specified by the start and Len parameters overlap the existing mapping null                       &N Bsp              , the overlapping parts will be discarded. If the specified start address is not available, the operation will fail.

FD: A valid file descriptor. Returns, by the general open () function, whose value can be set to-1. You need to specify the flags parameter as Map_anon, which indicates that anonymous mappings are in progress.

Offset: The starting point for the content of the mapped object.

Return value: Succeeded, returns a pointer to the mapped area; failed, returns-1.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/84/8F/wKiom1eUst6SGuJfAAB_gSzEC7s509.png "style=" float: none; "title=" 9.PNG "alt=" Wkiom1eust6sgujfaab_gszec7s509.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/84/8F/wKiom1eUst6BoA12AAAj-OFdaoY229.png "style=" float: none; "title=" 8.PNG "alt=" Wkiom1eust6boa12aaaj-ofdaoy229.png "/>



This article from "Together to see the Stars" blog, reproduced please contact the author!

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