I am looking to use POSIX message queues on a single process multi threaded application. mqueues will be used to share data between the threads.
I am a little confused on how they work in the Linux kernel. Do all the messages go through kernel space and then back to user space on the receive? a.k.a. from a userspace thread I do a mq_send and the message ends up in kernel space, and then on receive it is another system call to get the message from kernel space. If so isn't this highly inefficient for high-use message queues?
POSIX message queues are a means by which processes exchange data in the form of messages to accomplish their tasks. They enable processes to synchronise their reads and writes to speed up processes. POSIX message queues are distinct from System V messages.
In a queuing system, messages are stored at intermediate nodes until the system is ready to forward them. At their final destination they are stored in an electronic mailbox until the addressee is ready to read them.
System V IPC is older and POSIX IPC is newer. However there are some differences for some aspects. Not always Posix is better than System V. The semaphores, queues and shared memory for Posix have Ascii string names, while under System V these are given with integer number.
In a message queue, messages are stored in memory and are processed in the order that they are received. In a print queue, print jobs are stored in memory and are processed in the order that they are received. In a task queue, tasks are stored in memory and are processed in the order that they are received.
Yes, they will always go through the kernel (and were generally used for inter-process communication). If you just want inter-thread communication, you can probably do that via. a simple worker queue (using plain old mutexes).
If you want something with more features, you are almost certainly better off looking at something like AMQP.
Traditionally Unix/Linux has used sockets+read/write instead, but it depends on what you want (and how you want to use it).
Well I have to object to the notion that MQs are "highly" inefficient.
It is true that there is some overhead involved in copying data to/from the kernel and for truly high performance applications this might be a real consideration and reason to use shared or heap memory instead.
But short of well written shared memory code MQs are the fastest IPC around and come with substantial built-in facilities. The synchonization headaches are removed and (under linux at least) the message queue descriptor (mqd_t) can be used as a file descriptor in a select() statement. This allows for considerable flexiblity for doing something other than waiting on a mutex or constantly polling one. Additionally MQs are kernel persistent and that is a nice little feature if it is important that the queue data survive an application crash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With