Linux IPC Message Queuing

Source: Internet
Author: User
Tags message queue posix semaphore

1. Posix Message Queuing

/ * Mq_open-open a message queue * /
#include <fcntl.h>/*For O_* Constants*/#include<sys/stat.h>/*For Mode Constants*/#include<mqueue.h>mqd_t Mq_open (Const Char*name,intoflag); mqd_t Mq_open (Const Char*name,intOflag, mode_t mode,structMq_attr *attr);
/ * mq_send, mq_timedsend-send a message to a message queue */#include<mqueue.h>intMq_send (mqd_t mqdes,Const Char*msg_ptr, size_t msg_len, unsigned msg_prio); #include<time.h>#include<mqueue.h>intMq_timedsend (mqd_t mqdes,Const Char*msg_ptr, size_t msg_len, unsigned msg_prio,Const structTimespec *abs_timeout);
/ * mq_receive, mq_timedreceive-receive a message from a message queue */#include<mqueue.h>ssize_t mq_receive (mqd_t mqdes,Char*msg_ptr, size_t msg_len, unsigned*Msg_prio); #include<time.h>#include<mqueue.h>ssize_t mq_timedreceive (mqd_t mqdes,Char*msg_ptr, size_t msg_len, unsigned*Msg_prio,Const structTimespec *abs_timeout);
/* mq_getattr, Mq_setattr-get/set message Queue attributes */
#include <mqueue.h>intMq_getattr (mqd_t mqdes,structMq_attr *attr);intMq_setattr (mqd_t mqdes,structMq_attr *Newattr,structMq_attr *oldattr);structmq_attr {LongMq_flags;/*flags:0 or O_nonblock*/ Longmq_maxmsg;/*Max. # of messages on queue*/ LongMq_msgsize;/*Max. Message size (bytes)*/ LongMQ_CURMSGS;/*# of messages currently in queue*/ };
/* Mq_notify-register for notification if a message is available */
intMq_notify (mqd_t mqdes,Const structSigevent *SEVP);

/* Mq_unlink-remove a message Queue */
int mq_unlink (const char *name); Link with-lrt.

POSIX Message Queuing considerations

1. Mq_setattr can only modify mq_flags to be blocked or non-blocking, other parameters can only be specified by Mq_open when the message queue is established

2. Mq_receive returns the oldest message of the highest priority in the queue each time

3. Mq_notify supports registering a signal or thread, sending information when a message is added to an empty queue , or activating a thread

2. System V Message Queuing

/*msgget-get a System V message queue identifier*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intMsgget (key_t Key,intMSGFLG);/*Ftok-convert a pathname and a project identifier to a System V IPC key*/#include<sys/types.h>#include<sys/ipc.h>key_t Ftok (Const Char*pathname,intproj_id);/*MSGRCV, Msgsnd-system V message Queue operations*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intMSGSND (intMsqid,Const void*MSGP, size_t Msgsz,intmsgflg); ssize_t MSGRCV (intMsqid,void*MSGP, size_t Msgsz,LongMsgtyp,intMSGFLG);/*msgctl-system V Message control operations*/#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intMsgctl (intMsqid,intCmdstructMsqid_ds *buf);structMsqid_ds {structIpc_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*/unsignedLong__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)*/};

System v Message Queuing considerations

1. The flag parameter of the msgsnd can be specified as ipc_nowait. The IPC_NOWAIT flag makes this msgsnd operation non-blocking: if there is no storage space for new messages, the function immediately returns a Eagin error, as in the case of the following:

A. There are already too many bytes in the message queue (corresponding to msg_qbytes in the struct Msqid_ds)

B. There are too many messages in the system scope

If one of these two conditions occurs, and msgsnd does not specify the IPC_NOWAIT flag, the calling thread is put to sleep until:

A. Have space to store new messages

B. The specified message queue is eliminated (in this case msgsnd returns a ermid error)

C. The calling thread is interrupted by a captured signal (in which case MSGSND returns a eintr error)

2. The flag parameter of the MSGRCV can also be specified as ipc_nowait, and there is another flag msg_noerror: When the received message data portion is greater than the length parameter of MSGRCV, if Msg_ is set NoError, the data is truncated, the MSGRCV function is not faulted, and if the flag is not set, the MSGRCV function returns a e2big error.

3. The msgctl cmd can be evaluated as follows:

Ipc_rmid: Delete the specified queue

Ipc_set: Supports setting msg_prem.uid, Msg_prem.gid, Msg_prem.mode and msg_qbytes in the MSGID_DS structure of the specified queue

Ipc_stat: Returns the MSGID_DS structure of the specified queue

4. Limitations of system V Message Queuing

You can view the Msgmax Msgmni under the proc/sys/kernel/folder MSGMNB

You can also view it through the ipcs-l command:

Ubuntu:ipcs-L------Shared Memory Limits--------Max Number of segments=4096Max seg Size (Kbytes)=32768Max Total Shared memory (Kbytes)=8388608min seg size (bytes)=1------Semaphore Limits--------Max Number of arrays= -max semaphores per array= -Max semaphores system wide=32000Max Ops per semop call= +semaphore max Value=32767------Messages Limits--------max queues System wide=1717 System-wide maximum number of message queues: Msgmnimax size of message (bytes)=8192 maximum number of bytes per message: MsgmaxdefaultMax size of queue (bytes) =16384 Maximum number of bytes per message queue: MSGMNB

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