interprocess Communication---Message Queuing

Source: Internet
Author: User
Tags function prototype message queue

What is Message Queuing?

Message Queuing provides a way to send a block of data from a process to another process . Each block of data is considered to have a type, and the data blocks of the recipient process can have different type values.


Message Queuing differs from pipeline: Message Queuing is message-based, and pipelines are byte-stream based, and Message Queuing reads are not necessarily first-in-first-out.


Related functions

    1. Create a new message queue or get a message queue that already exists


      650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/7B/wKioL1eR0eqhIKXhAAAUWUWTU_0698.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1er0eqhikxhaaauwuwtu_0698.png "/>

      650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/7B/wKioL1eR0erx7K7oAAAZVXG3GxE525.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1er0erx7k7oaaazvxg3gxe525.png "/>

    2. Return value: Successful, returns the identifier of the message queue

Failed, return-1

The first parameter: The keyword of a Message Queuing object that the function compares to the keyword of an existing Message Queuing object to determine whether the message queue has been created. Derived from the function Ftok ().

The second parameter: the specific operation of the control function. You can take the following values:

Ipc_creat: If Message Queuing does not exist, it is created, otherwise it is opened directly.

IPC_EXCL: Use alone is meaningless.

Ipc_crear | IPC_EXCL: Message Queuing is newly created.


3. Ftok function

The function Ftok converts an existing pathname and an integer to a key_t value, called the IPC key.

key_t Ftok (const char* patnname,int ID);

Pathname: path must exist and can be accessed

ID: Low 8 bit


4. Read/write messages to the queue

MSGSND putting data into Message Queuing, MSGRCV fetching messages from the queue

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/7B/wKiom1eR1vWTNmx0AAAwxg03-Z0275.png "title=" 4.PNG " alt= "Wkiom1er1vwtnmx0aaawxg03-z0275.png"/>


Msqid: identifier of the message queue

MSGP: A pointer to the message buffer, which is used to temporarily store sent and accepted messages, which is a user-defined, generic structure.

MSGSZ: The size of the message

Msgtyp: The message pattern read from within the message queue. If 0, all messages in the message queue are taken out.

MSGFLG:MSGFLG is 0 o'clock, msgsnd () and MSGRCV () take a blocking wait processing mode when the queue is full or empty.

When MSGFLG and constants are in use, msgsnd () executes when the queue is full, MSGRCV () will not block, but returns-1, if the MSGRCV () is executed, waits for no return-1 if the message queue is rendered empty.


5. Set properties for Message Queuing

Function prototype: int msgctl (int msgqid,int cmd,struct msqid_ds *buf);

Performs a CMD operation on the message queue identified by the msgqid. The cmd operation is divided into three kinds: ipc_stat,ipc_set,ipc_rmid

Ipc_stat: This command is used to obtain the MSQID_DS data structure for the message queue and save it to the BUF specified address space.

Ipc_set: The command is used to set the properties of the message queue, and the properties to be set are stored in BUF.

Ipc_rmid: Removes the MSQID identity message queue from the kernel.


6. View Message Queuing ipcs-q

Delete Message Queuing ipcrm-q Semid


"Comm.h"

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/84/7C/wKioL1eR3NWQmrJ4AACWHzhf3fE800.png "style=" float: none; "title=" 5.PNG "alt=" Wkiol1er3nwqmrj4aacwhzhf3fe800.png "/>


"COMM.C"

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/84/7C/wKiom1eR3NWQV3zeAABrVaXgs1I529.png "style=" float: none; "title=" 7.PNG "alt=" Wkiom1er3nwqv3zeaabrvaxgs1i529.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/7C/wKioL1eR3NXRYI_CAABwBZ7GKrI414.png "style=" float: none; "title=" 8.PNG "alt=" Wkiol1er3nxryi_caabwbz7gkri414.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/7C/wKiom1eR3NayMK8eAABF5YXwqMw423.png "style=" float: none; "title=" 9.PNG "alt=" Wkiom1er3naymk8eaabf5yxwqmw423.png "/>

"Client.c"

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/7C/wKioL1eR3aOwLr3TAABlwPvpCEo142.png "style=" float: none; "title=" 11.PNG "alt=" Wkiol1er3aowlr3taablwpvpceo142.png "/>


"Server.c"

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/7C/wKiom1eR3aOCGOF5AABX_MSHtZM255.png "title=" 12.PNG "Style=" Float:none; "alt=" Wkiom1er3aocgof5aabx_mshtzm255.png "/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/84/7C/wKioL1eR3r2TRms8AAC_YmB5VG4209.png "title=" 13.PNG "alt=" Wkiol1er3r2trms8aac_ymb5vg4209.png "/>



650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/7D/wKiom1eR363z2m3-AAA8narvXh4120.png "title=" 15.PNG "alt=" Wkiom1er363z2m3-aaa8narvxh4120.png "/>

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

interprocess Communication---Message Queuing

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.