Linux Message Queue basics, linux Message Queue

Source: Internet
Author: User

Linux Message Queue basics, linux Message Queue
Basic concepts of Message Queue

Message Queue provides a method to send a piece of data from one process to another (limited to local)

Each data block is considered to be of a type. The data block received by the recipient process may have different types of values.

Message queues have the same limitations as queues, that is, the maximum length of each message is limited (MSGMAX), and the total number of bytes of each message queue is limited (MSGMNB ), the total number of message queues on the system also has an upper limit (MSGMNI)

 

MPs queue vs. Message Queue:

MPs queue message: boundary

First-in-first-out

 

Three restrictions on Message Queue size

Cat/proc/sys/kernel/msgmax # maximum message length limit

Cat/proc/sys/kernel/msgmnb # Total Message Queue bytes

Cat/proc/sys/kernel/msgmni # Number of message entries

 

 

IPC Object Data Structure

The kernel maintains a data structure for each IPC object.

struct ipc_perm{    key_t          __key;       /* Key supplied to msgget(2) */    uid_t          uid;         /* Effective UID of owner */    gid_t          gid;         /* Effective GID of owner */    uid_t          cuid;        /* Effective UID of creator */    gid_t          cgid;        /* Effective GID of creator */    unsigned short mode;        /* Permissions */    unsigned short __seq;       /* Sequence number */};

 

Message Queue-specific structure

struct msqid_ds{    struct ipc_perm msg_perm;     /* Ownership and permissions */    time_t          msg_stime;    /* Time of last msgsnd(2) */    time_t          msg_rtime;    /* Time of last msgrcv(2) */    time_t          msg_ctime;    /* Time of last change */    unsigned long   __msg_cbytes; /* Current number of bytes in                                                queue (nonstandard) */    msgqnum_t       msg_qnum;     /* Current number of messages                                                in queue */    msglen_t        msg_qbytes;   /* Maximum number of bytes                                                allowed in queue */    pid_t           msg_lspid;    /* PID of last msgsnd(2) */    pid_t           msg_lrpid;    /* PID of last msgrcv(2) */};

 

Message Queue representation in the kernel


Message Queue function example

Msgget Function 

Function: Creates and accesses a message queue.

Prototype:

int msgget(key_t key, int msgflg);

Parameters:

Key: The name of a message queue.

Msgflg: consists of nine permission tags, such as 0644. Their usage is the same as the mode flag used when creating a file (but the message queue does not have the x (execution) Permission)

 

Return Value:

A Message Queue ID is returned, that is, the ID code of the message queue.

 

Programming practices

// Practice 1: IPC_PRIVATE: Macro. The value is 0int main () {// the descriptor of the Message Queue created each time IPC_PRIVATE is different! // Therefore, the timestamp MessageID (key) is sent to another process (unless an associated process is involved). // other processes cannot use this message queue (except for lineage fork) int msgid = msgget (IPC_PRIVATE, 0666); if (msgid <0) {// if the message queue does not exist, an error is returned. errno = ENOENT if (errno = ENOENT) {cout <"ENOENT" <endl;} err_exit ("mesget error");} else {cout <"msgid =" <msgid <endl ;} return 0;}/** the Message Queue created by IPC_PRIVATE can only be used in processes related to the current process! */


// Practice 2: IPC_CREATint main () {// specify IPC_CREAT, the Message Queue int msgid = msgget (0x1235,066 6 | IPC_CREAT) will be created; if (msgid <0) {// if the message queue does not exist, an error is displayed. errno = ENOENT if (errno = ENOENT) {cout <"ENOENT" <endl ;} err_exit ("mesget error");} else {cout <"msgid =" <msgid <endl;} return 0 ;}


// Practice 3: IPC_CREAT | IPC_EXCLint main () {// specify IPC_EXCL. If yes, the report file already exists (error) int msgid = msgget (0x1235,066 6 | IPC_CREAT | IPC_EXCL); if (msgid <0) {err_exit ("mesget error ");} else {cout <"msgid =" <msgid <endl;} return 0 ;}




// Practice 4: Create with low permissions and enable int main () {// create int msgid = msgget (0x255, 0444 | IPC_CREAT) with high permissions; if (msgid <0) {err_exit ("mesget error");} else {cout <"Create Mes OK, msgid =" <msgid <endl ;} // open msgid = msgget (0x255,0644 | IPC_CREAT) with high permissions; if (msgid <0) {err_exit ("mesget error ");} else {cout <"Create Mes OK, msgid =" <msgid <endl;} return 0 ;}


Msgget function parameter Relationship Diagram

 

 

Msgctl Function

Function: Controls message queues.

Prototype:

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

Parameters:

Msqid: ID of the Message Queue returned by the msgget Function

Cmd: the action to be taken (see below)

 

Return Value:

0 is returned for success and-1 is returned for failure.

 

Cmd: the action to be taken (three values are available), which are as follows:


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.