Message Queuing status: struct MSQID_DS

Source: Internet
Author: User
Tags message queue

The Linux message Queue (queue) is essentially a linked list that has a message queue identifier (queue ID). Msgget Create a new queue or open a queue that exists; Msgsnd adds a new message to the end of the queue; MSGRCV the message from the queue, the message is not necessarily followed by FIFO, or by Message Type field.


1. Identifiers (DES) and Keys (key):


Message queues, semaphores, and shared storage segments are all part of the IPC architecture in the kernel, and are described by identifiers. This identifier is a non-negative integer, unlike a file descriptor, which is created without reusing an integer that is recycled by deleting it, but at +1 each time until the integer maximum is turned to 0.


The identifier is the internal name of the IPC object, and its external name is the key (the key), and its basic type is key_t, defined as a long integer in the header file <sys/types.h>. The key is transformed from the kernel into an identifier.


2. Message Queuing status Msqid_ds:


Each message queue has a MSQID_DS structure associated with it:




struct MSQID_DS
{
struct Msqid_ds {
struct Ipc_perm msg_perm;
struct MSG *msg_first; /* The queue,unused/*
struct MSG *msg_last; /* Last Message in queue,unused * *
__kernel_time_t Msg_stime; /* Last msgsnd time * *
__kernel_time_t Msg_rtime; /* Last MSGRCV time * *
__kernel_time_t Msg_ctime; /* Last Change time * *
unsigned long msg_lcbytes; /* Reuse junk fields for bit *
unsigned long msg_lqbytes; * Ditto * *
unsigned short msg_cbytes; /* Current number of bytes on queue * *
unsigned short msg_qnum; /* Number of messages in queue * *
unsigned short msg_qbytes; /* max number of bytes on queue */
__kernel_ipc_pid_t Msg_lspid; /* PID of last MSGSND * *
__kernel_ipc_pid_t Msg_lrpid; /* Last receive PID * *
};




3. Generate a key by path name and project ID:


If the client process and the server process identify a path name and a project ID (character value between 0~255), then call Ftok to transform the two values into a key.


Prototype: key_t ftok (const char *path, int id);
Header file: <sys/ipc.h>
Return value: Success returns key, Error returns (key_t)-1.
Parameter: The path parameter must refer to an existing file. Use only the lower 8 bits of the ID parameter when the key is generated.
Note: If two path names refer to two different files, calling Ftok on both path names usually returns a different key. However, because the I node number and key are usually stored in a long integer, the information may be lost when the key is created. This means that if you use the same project ID, you may have the same key for the two pathname of a different file. This function works in the following ways:
Gets the stat structure of the given path name.
Remove some St_dev and St_ino fields from the structure and combine them with the project IDs.
4. Create/Open message queues:


Msgget can create a new queue or open a queue that exists.


Prototype: int msgget (key_t key, int flag);
Header file: <sys/msg.h>
Return value: Success returns the message queue ID, error returns-1.
Parameters:
Key: The key value of the message queue.
Flag: Sign bit.
Description
There are two ways to create queues:
Key is ipc_private.
Key is not currently combined with a specific type of IPC structure, and the ipc_creat bit is specified in flag.
Initialize MSQID_DS Members:
The mode member in the Ipc_perm is set by flag.
Msg_qnum, Msg_lspid, Msg_lrpid, Msg_stime and Msg_rtime are all set to 0.
Msg_ctime is set to the current time.
Msg_qbytes is set to system limit values.
5. Message Queuing garbage bucket function:


Msgctl similar to the IOCTL function in a driver, you can perform a variety of actions on Message Queuing.


Prototype: int msgctl (int msqid, int cmd, struct msgqid_ds *buf);
Header file: <sys/msg.h>
Return value: Returns 0 for success, or 1 for error.
Parameters: The cmd parameter describes the command to be executed for the queue specified by the msqid:
Ipc_stat: Takes the MSQID_DS structure of this queue and stores it in the structure that the BUF points to.
Ipc_set: Sets the Msg_perm.uid, Msg_perm.gid, Msg_perm.mode, and msg_qbytes in the structure associated with this queue by buf pointing to the values in the structure. Only the following two processes can execute this command:
The valid user ID equals Msg_perm.cuid or msg_per.uid.
A process that has superuser privileges.
Ipc_rmid: Deletes the message queue and all data that is still in the queue from the system. Execution Permissions Ibid.
6. Put data in Message Queuing:


Call msgsnd to put the data in the message queue.


Prototype: int msgsnd (int msqid, const void *ptr, size_t nbytes, int flag);
Header file: <sys/msg.h>
Return value: Returns 0 for success, or 1 for error.
Description: You can define a message structure with a type in the structure, so that the message may be fetched in a non advanced first order. When Msgsnd returns successfully, the MSQID_DS structure associated with Message Queuing is updated to indicate the process ID (MSG_LSQID) that issued the call, the time that the call was made (Msg_stime), and indicate that a message (Msg_qnum) was added to the queue.
7. Fetching messages from message queues:


Calling MSGRCV will fetch the message from the message queue.


Prototype: ssize_t MSGRCV (int msqid, void *ptr, size_t nbytes, long type, int flag);
Header file: <sys/msg.h>
Return value: Success returns the length of the data portion of the message, and the error returns-1.
Parameters:
PTR: Points to a long integer (the returned message type is stored in it) followed by a buffer that holds the actual message data.
Nbytes: The length of the data buffer. If the message returned is greater than Nbytes and Msg_noerror is set in flag, the message is truncated.
Type
Type = 0: Returns the first message in the queue.
Type > 0: Returns the first message of type type in the queue.
Type < 0: Returns a message in the queue in which the message type value is less than or equal to the type absolute, and if there are several, a message with the smallest type value.
Note: When MSGRCV returns successfully, the MSQID_DS structure associated with Message Queuing is updated to indicate the caller's process ID (msg_lrpid), the call time (Msg_rtime), and the number of messages in the queue (Msg_qnum) minus 1.
Four, Message queue application instance
Message Queuing is relatively simple to use, and the following example basically covers all operations on Message Queuing, while the output of the program helps to deepen understanding of some of the rules and message queue restrictions previously described.


#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
void Msg_stat (Int,struct msqid_ds);
Main ()
{
int gflags,sflags,rflags;
key_t key;
int msgid;
int Reval;
struct msgsbuf{
int mtype;
Char mtext[1];
}msg_sbuf;
struct MSGMBUF
{
int mtype;
Char mtext[10];
}msg_rbuf;
struct Msqid_ds msg_ginfo,msg_sinfo;
char* msgpath= "/unix/msgqueue";
Key=ftok (Msgpath, ' a ');
Gflags=ipc_creat| IPC_EXCL;
Msgid=msgget (key,gflags|00666);
if (msgid==-1)
{
printf ("msg create error\n");
Return
}
Output Message Queuing default properties after a message queue is created
Msg_stat (Msgid,msg_ginfo);
sflags=ipc_nowait;
msg_sbuf.mtype=10;
Msg_sbuf.mtext[0]= ' a ';
Reval=msgsnd (Msgid,&msg_sbuf,sizeof (Msg_sbuf.mtext), sflags);
if (reval==-1)
{
printf ("Message send error\n");
}
Output Message Queuing properties After a message is sent
Msg_stat (Msgid,msg_ginfo);
Rflags=ipc_nowait| Msg_noerror;
REVAL=MSGRCV (Msgid,&msg_rbuf,4,10,rflags);
if (reval==-1)
printf ("Read msg error\n");
Else
printf ("read from msg queue%d bytes\n", Reval);
Output Message Queuing properties After a message is read from a message queue
Msg_stat (Msgid,msg_ginfo);
Msg_sinfo.msg_perm.uid=8;//just a try
msg_sinfo.msg_perm.gid=8;//
msg_sinfo.msg_qbytes=16388;
This verifies that Superuser can change the default msg_qbytes for Message Queuing
Note that the value set here is greater than the default value
Reval=msgctl (Msgid,ipc_set,&msg_sinfo);
if (reval==-1)
{
printf ("msg set info error\n");
Return
}
Msg_stat (Msgid,msg_ginfo);
Verifying setting Message Queuing properties
Reval=msgctl (msgid,ipc_rmid,null);//Delete message queue
if (reval==-1)
{
printf ("Unlink msg queue error\n");
Return
}
}
void Msg_stat (int msgid,struct msqid_ds msg_info)
{
int Reval;
Sleep (1);//Only for the convenience of the back output time
Reval=msgctl (Msgid,ipc_stat,&msg_info);
if (reval==-1)
{
printf ("Get msg info error\n");
Return
}
printf ("\ n");
printf ("Current number of bytes on \%d\n", msg_info.msg_cbytes);
printf ("Number of messages in the queue is%d\n", msg_info.msg_qnum);
printf ("max number of bytes on \%d\n", msg_info.msg_qbytes);
The capacity of each message queue (in bytes) is limited MSGMNB, and the size of the value varies by system. When you create a new message queue, the default value for//msg_qbytes is MSGMNB
printf ("pid of last Msgsnd is%d\n", msg_info.msg_lspid);
printf ("pid of last Msgrcv is%d\n", msg_info.msg_lrpid);
printf (' Last msgsnd ' time is%s ', CTime (& (Msg_info.msg_stime)));
printf (' Last Msgrcv ' time is%s ', CTime (& (Msg_info.msg_rtime)));
printf ("Last Change Time is%s", CTime (& (Msg_info.msg_ctime));
printf ("Msg uid is%d\n", msg_info.msg_perm.uid);
printf ("msg gid is%d\n", msg_info.msg_perm.gid);
}

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.