interprocess communication (Shared memory), five kinds of communication methods simple summary

Source: Internet
Author: User
Tags message queue

Shared Memory: It is the most efficient communication mechanism in the System V version that allows multiple processes to share the same address space, and if a process modifies that address space, other processes that share the address space can see it at the same time, but it does not provide synchronization and mutex. The general combination of semaphores to achieve synchronization between the process of mutual exclusion.

Create shared memory: int shmget (key_t _key,size_t size,int SHMFLG);//size must be a multiple of 4096, which is a multiple of the page table size.

Hook up process: void* shmat (int shmid,const void* shmaddr,int shmflg);//Attach the current call to the shared memory process to the specified address space. General Settings Shmflg default to 0,SHMADDR is generally null

To associate a process: int shmdt (const void *SHMADDR)//Simply modify the page table directly

Release shared memory: int shmctl (int shmid,int cmd,const void* buf),//cmd is immediately removed with ipc_rmid directive.

Share.h

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/17/wKioL1cTh62S-blUAABFzihbuBo239.png "title=" Share.h.png "alt=" Wkiol1cth62s-bluaabfzihbubo239.png "/>

SHARE.C file

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/21/wKioL1cUqkyhxHa3AABP7QaU8fA407.png "style=" float: none; "title=" Share.c1.png "alt=" Wkiol1cuqkyhxha3aabp7qau8fa407.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/21/wKioL1cUqk2hZW_qAAAx_xAI5es298.png "style=" float: none; "title=" Share2.c.png "alt=" Wkiol1cuqk2hzw_qaaax_xai5es298.png "/>

CLIENT.C file

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/23/wKiom1cUqY-A9V8NAAA7BJlvELo762.png "style=" float: none; "title=" Client.c.png "alt=" Wkiom1cuqy-a9v8naaa7bjlvelo762.png "/>

SERVER.C file

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/21/wKioL1cUqk3AwAHFAAAnvy8HqRQ358.png "style=" float: none; "title=" Server.png "alt=" Wkiol1cuqk3awahfaaanvy8hqrq358.png "/>

Operation Result:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/23/wKiom1cUqZDQ4V4IAABAJbWxM4w989.png "style=" float: none; "title=" result. png "alt=" Wkiom1cuqzdq4v4iaabajbwxm4w989.png "/>

Five ways to communicate between processes Summary: The reason for communication between processes is actually to make each other's data accessible to each other.

Pipeline communication: Anonymous pipeline (only applies to one-way communication between the blood processes, it is a special file in memory); Named pipes (resolves two unrelated processes can also be unidirectional communication, it is a real on the hard disk of the file, in the file system in the form of path name. They are all byte-based.

Three communication mechanisms under the System V version:

1. Message Queuing (enables two processes to communicate in two directions, i.e. a one-answer form). One process can add a message to the queue, and another process can read the message from the message queue.

2. Semaphore (through the p,v operation to the current critical resource access to orderly control access). The primary role is synchronization between multiple threads in the process or in the same process.

3. Shared memory (that is, multiple processes can share the contents of an address space at the same time and modify its contents, the process can be seen by other processes that share the same address space), which is most efficient.


interprocess communication (Shared memory), five kinds of communication methods simple summary

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.