POSIX Message Queuing for Linux interprocess communication

Source: Internet
Author: User

POSIX Message Queuing is similar to the use of system V Message Queuing, with the following major differences:
1. The read of the POSIX message queue always returns the oldest message with the highest priority, and the read to the System V message queue returns the specified priority message.
2. Posix Message Queuing allows you to generate a signal or start a thread to write a message to an empty queue, and System v Message Queuing is not available.

POSIX Message Queuing common functions and header files
#include <mqueue.h>
1. mqd_t mq_open (const char *name,int oflag,.../* mode_t mode, struct mq_attr *attr */);
Return value: Returns the message queue descriptor if successful, returns 1 if an error occurs

Name: For Path
Oflag: One of o_rdonly, o_wronly or O_RDWR, also can be bitwise OR on o_creat,o_excl,o_nonblock.
When you use O_creat to create a new message queue and the new message queue does not exist, the parameters mode and attr are required.
Mode: The user rights that belong to
attr: Used to assign certain properties to a new queue, or to a null pointer, which is the default property.
The return value of Mq_open is used for the first parameter of other POSIX Message Queuing functions

2. int Mq_close (mqd_t mqdes);
Return value: Successfully returned 0, error returned-1
This function is to close the message pair column that the descriptor points to for the process, but it is not removed from the system.

3. int Mq_unlink (const char *name);
Return value: Successfully returned 0, error returned-1
This function is to remove the message pair column that the name points to in the system.

4. int mq_getattr (mqd_t mqdes, struct mq_attr *attr);
int mq_setattr (mqd_t mqdes, const struct MQ_ATTR *attr,struct mq_attr *oattr);
Return value: Successfully returned 0, error returned-1
The MQ_ATTR structure has the following properties:
struct mq_attr{
Long mq_flags://Message Queue flag: 0,o_nonblock
Long mq_maxmsg; Maximum number of message bars allowed in Message Queuing
Long mq_msgsize; Maximum number of bytes in each message
Long mq_curmsgs; Current number of messages in Message Queuing
};

The pointer to the MQ_ATTR structure can be passed as the fourth parameter of the Mq_open (mq_attr *attr), allowing the mq_maxmsg and Mq_msgsize properties to be specified when creating a new POSIX message queue, and the other two idiom attributes are mq_open ignored.

Mq_getattr: Gets the current properties of the message queue and fills in the structure that attr points to.
Mq_setattr: Sets the properties of the specified message queue, but only uses the Mq_flags member to set or clear the fee blocking flag. The other three member properties are ignored (mq_maxmsg and mq_msgsize can only be specified when created, MQ_CURMSGS can only get cannot be set).

5.   int mq_send (mqd_t mqdes, const char *ptr, size_t len, unsigned int prio);        ;      Return value: Successfully returned 0, error returned-1

       ssize_t mq_recieve (mqd_t mqdes, Char *ptr, size_t Len, unsigned int *priop);
             return Value: Success returns the number of bytes in the message, error returns-1


Mq_ The value of the Len parameter in receive cannot be less than the mq_msgsize of the MQ_ATTR structure, otherwise mq_receive immediately returns a emsgsize error. The PRIO parameter in the
Mq_send is the priority of the write message and must be less than Mq_prio_max. If the mq_receive Priop is a non-null pointer, the priority of the returned message is stored. If you do not use a priority, you can specify a priority of 0,                    mq_send       mq_receive Specifies that the last parameter is a null pointer.

Code Example:

Creating POSIX message-to-column mq_creat.c

    1. #include "stdio.h"
    2. #include "Stdlib.h"
    3. #include "mqueue.h"
    4. int main (char Argc,char **argv)
    5. {
    6. mqd_t MQDC;
    7. if (argc>1)
    8. {
    9. Mqdc=mq_open (argv[1],o_creat| o_rdwr| O_wronly,0666,null);
    10. if (mqdc==-1)
    11. printf ("Open error\n");
    12. Mq_close (MQDC);
    13. }
    14. return 0;
    15. }
Copy Code



POSIX message-to-column send code MQ_SEND.C

  1. #include "Stdlib.h"
  2. #include "stdio.h"
  3. #include "mqueue.h"
  4. int main (char Argc,char **argv)
  5. {
  6. mqd_t Mqds;
  7. void *ptr;
  8. size_t Len;
  9. unsigned int prio;
  10. if (argc<3)
  11. printf ("Usage:./send <name> < #bytes > <priority>\n");
  12. Else
  13. {
  14. Len=atoi (argv[2]);
  15. Prio=atoi (Argv[3]);
  16. Mqds=mq_open (argv[1],o_wronly);
  17. if (mqds==-1)
  18. printf ("Open error\n");
  19. Ptr=calloc (len,sizeof (char));
  20. Mq_send (Mqds,ptr,len,prio);
  21. Mq_close (MQDS);
  22. }
  23. return 0;
  24. }
Copy Code

POSIX Message Queuing receive code MQ_RECEIVE.C

  1. #include "Stdlib.h"
  2. #include "stdio.h"
  3. #include "mqueue.h"
  4. int main (char Argc,char **argv)
  5. {
  6. mqd_t MQDR;
  7. ssize_t N;
  8. unsigned int prio;
  9. void *buff;
  10. struct mq_attr attr;
  11. if (argc>1)
  12. {
  13. Mqdr=mq_open (argv[1],o_rdonly);
  14. if (mqdr==-1)
  15. printf ("Open error\n");
  16. Mq_getattr (MQDR,&ATTR);
  17. Buff = malloc (attr.mq_msgsize);
  18. while ((N=mq_receive (Mqdr,buff,attr.mq_msgsize,&prio)) <=0)
  19. {
  20. printf ("wait\n");
  21. Sleep (1);
  22. }
  23. printf ("Buff is%s,n=%d,prio=%d\n", Buff,n,prio);
  24. Mq_close (MQDR);
  25. }
  26. return 0;
  27. }
Copy Code



Operation Result:

Run creat first to create a new message queue: $./creat/mq_test

Then run send message:
$./send/mq_test 20 2
$./send/mq_test 80 8
$./send/mq_test 40 4
$./send/mq_test 60 6


Last run receive receives message:
$./receive/mq_test
Buff is, n=80,prio=8
$./receive/mq_test
Buff is, n=60,prio=6
$./receive/mq_test
Buff is, n=40,prio=4
$./receive/mq_test
Buff is, n=20,prio=2


From the running results, the sending message program sends a priority to 2,8,4,6 length of 20,80,40,60 four messages, and then runs the receiving program first receives a priority of up to 8, a length of 80 messages, by priority from high to low received.
Because the sent data is calloc out of random data, print results can not be displayed, can only show "Buff is".

6. int mq_notify (mqd_t mqdes, const struct sigevent *notification);
Return value: Successfully returned 0, error returned-1

This function establishes or deletes an asynchronous event notification for a specified POSIX message queue.

Note the point:
1) Only one process can register to receive a Message Queuing notification at any time.
2) When the notification is received, the existing registration is revoked, and if it needs to be received again, it needs to be registered again.
3) The blocking priority of the process mq_receive is greater than the priority of the registered receive notification.

POSIX Message Queuing for Linux interprocess communication

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.