IPC Message Queuing

Source: Internet
Author: User

Message Queuing stores the message in the kernel in a linked list, calls the MSGSND,MSGRCV function to deliver it to the message queue, and extracts the specified message.

    • Create a message queue

Generate a message queue or get an existing message queue ID

       #include <sys/types.h>       <sys/ipc.h>       <sys/msg.h>       int int MSGFLG);

The Msgget function returns the message queue ID corresponding to the key value.

1. Key is a thing that corresponds to an IPC object and acts as an identifier in the kernel.

2. The returned ID acts as an external identifier for our application layer, such as all functions that operate Message Queuing, using MsgId to uniquely identify a message queue.

The 3.MSGFLG is used to specify the permissions of the message queue, the operation properties, the operation attributes, the status of the operation permissions, such as the MSGFLG commonly used high value:

    ipc_creat: Used to create a message queue

    ipc_excl: Query The message queue specified by key to release presence

    ipc_nowait: The message queue operation after is non-blocking

An example of Msgget (key, ipc_creat|  0666); Creates a message queue specified by key, with an operation permission of 0666.

id = msgget (key, 0666) gets the ID of the message queue corresponding to the key value.

When the key value is ipc_private, or the key value is not for it but MSGFLG specifies ipc_creat, a new message queue is created if the message queue does not exist. when you specify Ipc_private as the key value, a new message queue is always created, and the generated Message Queuing key value is 0.

This shows that getting the message queue Operation ID is critical, and there are generally 3 ways to get the ID:

1. Specify a key value of ipc_private , create a new message queue, let the ID value write to a file, another process read the file, get the ID value, so that 2 processes can communicate through this message queue.

2. Manually specify a value for the key value, more than one process to see the key, the problem is that there may be a key value and the specified key value of the same message queue exists, you need to handle such an error, to replace a key value.

3. Using the Ftok function to generate a key, the same key is called Msgget the ID obtained is definitely the same.

       #include <sys/types.h>       <sys/ipc.h>       key_t ftok (constchar  int proj_id); (PROJ_ID is 1-255)

The implementation of the Ftok function is to generate a key value by using the STAT function to obtain the St_dev,st_ino member (partial bit) and PROJ_ID (only low 8 bits) of the pathname, so as long as pathname and proj_id are determined, Then the key value is basically determined.

1. But this function still exists one possibility is that pathname is not the same, proj_id, like, still get the same key, this is because St_dev,st_ino member data is truncated, probably just the same data is preserved.

2. There is also a need to ensure that pathname this file will not be changed throughout the process, otherwise, after the a processes get key, before the B process gets key, modify pathname this file, thus affecting the St_dev,st_ino, resulting in B to get a different key and a, Although the pathname,proj_id has not changed.

    • Message Queuing Property Description

Can get, set, delete message queue via Msgctl

       #include <sys/types.h>       <sys/ipc.h>       <sys/msg.h>       int Msgctl (intintstruct msqid_ds *buf);

It is used like an IOCTL in IO operations. CMD has the following values:

Ipc_stat: Gets the description structure of the message queue specified by MsgId, stored in BUF.

Ipc_set: Sets the description structure of Message Queuing

Ipc_rmid: Immediately deletes the message queue for the specified key value, and the key value is stored in the BUF structure. If there are still processes reading and writing to this message queue after deletion, a EIDRM error is returned.

In general, if a Message Queuing error occurs, use Ipc_rmid to delete the message queue and release the resources it occupies in the kernel.

IPC 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.