Linux Message Queuing Concepts

Source: Internet
Author: User

Concept of 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 be of a type, and the receiver process receives a block of data that can have different type values. We can avoid problems with the synchronization and blocking of named pipes by sending messages. Message Queuing differs from the pipeline in that Message Queuing is message-based, and pipelines are byte-stream based, and Message Queuing reads are not necessarily first-in-first-out. Message Queuing and Named pipes have the same disadvantage that the maximum length of each message is capped (Msgmax), the total number of bytes per message queue is capped (MSGMNB), and the total number of message queues on the system has an upper limit (Msgmni).

IPC Object Data structure:

struct IPC_PERM

{

key_t __key;

uid_t uid;

gid_t GID;

uid_t cuid;

GID) T Cgid;

unsigned short mode;

unsigned short short__seq;

};

Create a new message queue or get a message queue that already exists:

int Msgget (key_t key,int MSGFLG)

Key: Can be considered as a port number, can also be generated by the function Ftok;

MSGFLG:

Ipc_creat: If the IPC does not exist, create an IPC resource, otherwise open the operation.

IPC_EXCL: A new memory share is created only if the shared memory does not exist, or an error occurs.

If you use the Ipc_creta,xxxget () function alone either returns an operator of the shared memory that already exists, or returns an identifier for the newly created shared memory.

If the Ipc_creta and IPC_EXCL flags are used together, Xxxget () returns a new IPC identifier, or 1 if the IPC resource exists.

The IPC_EXEL flag itself does not make much sense, but it can be used with the IPC_CREAT flag to guarantee that the resulting object is new, rather than opening an existing object.

Read/write message to queue:

Read message: ssize_t magrcv (int msqid,void* msgp,size_t masgsz,long msgtyp,int msgflg);

Write message: int msgsnd (int msqid,const void* msgp,size_t msgsz,int msgflg);

Parameters:

MSGQID: The identification code of the message queue;

MSGP: A pointer to the message buffer, which is used to temporarily store messages sent and received, is a generic structure that can be defined by a user,

struct MSGSTRU

{

Long mytype;//greater than 0

Char mtext[user-specified size];

};

MSGSZ: The size of the message;

Msgtyp: The message pattern read from within the message queue. A value of zero indicates that all messages in the message queue will be read;

MSGFLG: Used to indicate the action that the core program should take if the queue has no data. If the MSGFLG and constant ipc_nowait are pooled, then if the message queue is full at msgsnd () execution, then MSGSND () will not block, but immediately return-1, if the MSGRCV () is performed, then when the message queue is empty, do not wait for the immediate return-1, and set the error code to enomsg. When MSGFLG is 0 o'clock, msgsnd () and MSGGRC () take a blocking wait processing mode when the queue is empty.

To set the properties of a message queue:

int msgctl (int msgqid,int cmd,struct msqid_ds* buf)

Parameters:

The MSGCTL system invokes a CMD operation on the queue message identified by the MSGQID, and the system defines 3 types of CMD operations: Ipc_stat,ipc_set,ipc_rmid

Ipc_stat: This command is used to obtain the MSGQID_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 MSGQID identity message queue from the kernel.

Ftok function: Converts an existing pathname and an integer identifier to a key_t value, called the IPC key.

key_t Ftok (const char* pathname,int proj_id)

The typical implementation of Ftok calls the stat function and then combines three values:

1. Information on the file system where the pathname resides (St_dev member of the stat structure);

2. The index node number of the file in this system (St_ino member of the stat structure)

3, PROJ_ID Low-order 8-bit (cannot be 0)

There is no guarantee that two different path names will produce different keys than the combination of the same proj_id, because the number of information bits in the three entries listed above (file system identifier, index node, proj_id) may be greater than the number of bits in an integer.


This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1825705

Linux Message Queuing Concepts

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.