Inter-process Communication (IPC) ———— shared memory

Source: Internet
Author: User

A. Shared memory

In the system, two different processes will maintain their own piece of address space, this address space is generally a virtual address, will be mapped through the MMU and page table to the corresponding physical memory, because different processes will have different memory space, so two processes are not able to see each other's data, The shared memory is to enable two processes to see the same address space, so as to achieve data interaction between different processes.

It is worth proposing that shared memory is one of the most efficient ways to communicate between processes, because it is the exchange of data directly by accessing memory, eliminating the amount of time required to perform p and V operations in the replication of data in Message Queuing and in semaphores.


Two. Functions in shared memory

    1. Creation and destruction of shared memory

Create:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/0D/wKiom1cQ0KLQv4yVAAAhJ507Cvc036.png "title=" create. png "alt=" Wkiom1cq0klqv4yvaaahj507cvc036.png "/>

function parameters,

The key value is needless to say, is created with the Ftok function, you need to use the path name and a proj_id integer;

size refers to how much space is required, the data in Linux is divided by page, 1 pages is 4K is also 4,096 bytes, the size of the alignment number is an integer multiple of 4096, that is, if you specify 4,098 bytes, the system will be allocated 4096* 2 of the size of the space;

The SHMFLG is the same as the message queue and the semaphore, with the ipc_creat and ipc_excl two options, and using both values creates a new shared memory, which returns 1 if the shared memory exists. , and when Ipc_creat is used alone, the shared memory ID is returned if the shared memory is present, otherwise it will be newly created;


Destroyed:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/0A/wKioL1cQ1KCCRaLvAAAfkTvT_hc249.png "title=" destroy. png "alt=" Wkiol1cq1kccralvaaafktvt_hc249.png "/>

function parameters,

Shmid is the ID of the shared memory created, and the cmd option can be ipc_rmid, and the following buf is a struct pointer, where the shared memory needs to be destroyed and the BUF can be set to NULL;


2. Association and cancellation of shared memory

Once the shared memory is created, it is necessary to associate the process with the shared memory to be able to use it, as well as to disassociate the association when it is finished:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/0D/wKiom1cQ1cvg8YayAAAK-S-JpDw438.png "title=" at &dt.png "alt=" Wkiom1cq1cvg8yayaaak-s-jpdw438.png "/>

In

Shmat is to associate a process with shared memory attach,Shmid is the ID of the shared memory, and the subsequent shmaddr is a pointer to a buffer, if NULL, The system will automatically select a suitable memory as a buffer, and SHMFLG can be shm_rdonly to indicate that the process is read-only in shared memory, and when SHMFLG is 0, it is possible to read and write, and there is no write-only option;

You can see that the return value of the Shmat function is a void-type pointer, which can be understood as a pointer to a space returned in memory;

shmdt function parameters only one, open up the memory of course also to release, so shmaddr is the return value of Shmat, used to cancel the association;


Chestnut Time:


As with the message queues and semaphores previously discussed, you can encapsulate functions in shared memory:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/0A/wKioL1cQ2YXwyzM6AACH97l-kxQ304.png "title=" only. png " alt= "Wkiol1cq2yxwyzm6aach97l-kxq304.png"/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/0A/wKioL1cQ2QWRlSg1AAAizeCLqLo549.png "style=" float: none; "title=" QQ picture 20160415201123.png "alt=" Wkiol1cq2qwrlsg1aaaizeclqlo549.png "/>


This allows the creation of two processes shm_client.c and SHM_SERVER.C:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/0D/wKiom1cQ3VGw6R2oAABmi5rzsVQ294.png "title=" QQ picture 20160415203039.png "alt=" Wkiom1cq3vgw6r2oaabmi5rzsvq294.png "/>


Two processes are associated with the created shared memory, Shm_client.c writes the data to the shared memory, and SHM_SERVER.C prints the data out of it, so that the interprocess communication is implemented, running the program:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7F/0A/wKioL1cQ3yDjpCj6AAAqApmAE6s733.png "title=" result. png "alt=" Wkiol1cq3ydjpcj6aaaqapmae6s733.png "/>


One thing to say here is that the semaphore mentioned in the previous article can actually be used in conjunction with shared memory, because when two processes simultaneously access a piece of shared memory to write data to or read data, the shared memory can be viewed as a critical resource with a semaphore as the identifier. You can complete the synchronization and mutex issues between processes.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1764314

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.