RishiD
RishiD

Reputation: 2168

POSIX Message Queue go through kernel space?

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?

Upvotes: 7

Views: 4959

Answers (2)

Duck
Duck

Reputation: 27542

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.

Upvotes: 6

James Antill
James Antill

Reputation: 2855

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

Upvotes: 7

Related Questions