Notes on the Linux implementation of mqueues (POSIX Message Queues)

(a.k.a A Very Unofficial Linux mqueue HOWTO)

By James Macfarlane

Version 0.1 2005-12-17

0. Quick-start

For the very impatient:

1. Introduction

There appears to be very little documentation on the linux mqueue implementation (unless you go and read the kernel source, I guess). A google search (in December 2005) seems to indicate that there is no HOWTO-level document available which covers everything you need to get POSIX mqueues working on Linux. This file is not meant as a replacement for a proper "HOWTO" on the subject. These are just some things which I have discovered by trial and error and by searching the Net and they may be helpful to you so I'm sharing them in this document.

Disclaimer: this document is not meant to cover anything about what mqueues are, how they work or how to use them. Chances are, if you want mqueues, then you've already got a very good reason to use them since there are many other IPC methods available under linux including System V queues, pipes, FIFOS, shared memory, etc. If you do need more information on the subject, I thoroughly recommend Unix Network Programming (see below). Please don't ask me, I'm no expert on IPC.

Assumptions: I assume the reader is a reaonably competant linux user who would have no problem in configuring, compiling and installing a new kernel (these subjects are covered very thoroughly elsewhere).

2. What's involved

There are 4 steps to getting mqueue support up-and-running on your linux box:

These steps are covered below.

2.1 Enabling kernel support for mqueues

POSIX queue support is a recent introduction to the kernel. It has been included in the kernel since version 2.6.6-rc1 so you need this or something later. Personally, I've tried it with 2.6.10 and 2.6.12.

To enable mqueue support, you firstly need to turn on prompting for experimental code (CONFIG_EXPERIMENTAL=y) if you haven't already done this. If you are using 'make menuconfig' this is found under:

    Code maturity level options  -->
        [ ] Prompt for development and/or incomplete code/drivers

You then need to select CONFIG_POSIX_MQUEUE which is found under:

    General setup  -->
        [ ] POSIX Message Queues

Now build and install the new kernel.

If you're not running the new kernel or have not successfully enabled mqueue support, then you'll get a "Function not implemented" (ENOSYS) error when you try to use any of the mq system calls.

2.2 Mounting the mqueue filesystem

The Linux mqueue driver implements a filesystem. You must mount this filesystem before you can create any mqueues. There is only one place the kernel / libraries know about for location of the mount point and that is /dev/mqueue. Note: this is not a device node - think of it like /dev/pts or /dev/shm.

The mount(8) manpage does not know about mqueues (well, not on Debian testing at the time of writing anyway) - mqueue support is too recent for that. So, you need to know that the mount type is mqueue (which makes sense) but there is no device node to mount - you specify it as none. So, before you can use mqueues you need to do the following as root:

    mkdir /dev/mqueue
    mount none /dev/mqueue -t mqueue

You then refer to the mqueues in your code as "/my_queue_name" . You must not include the whole path name (e.g. /dev/mqueue/my_queue_name).

In order to allow users other than root to use the mqueues it might be a good idea to do this:

    chmod 775 /dev/mqueue
    chown root:staff /dev/mqueue     # or whatever group you need

If you want to ensure the mqueue filesystem is always available on boot-up, edit your /etc/fstab to include the following line:

    none    /dev/mqueue     mqueue      defaults    0   0

2.3 Library support for message queues

As of 2.3.4, glibc contains support for message queues within librt, so if you've got this version of glibc or later, then you won't need to do anything extra at this stage.

If you have an earlier version of libc that doesn't offer mqueue support, you can download a stand-alone mqueue library from Michal Wronski's site - he's the author of Linux mqueues. (note: it's on Geocities: there be pop-up ads)

Note: according to the kernel config help, this library is hosted at: www-users.mat.uni.torun.pl/~wrona/posix_ipc/. However, it has now been moved to the URL given above.

The library is built with the usual process of untar, ./configure, make, make install.

The library also contains some example code and man pages for the various mq_ calls.

2.4 Linking your program against the libraries

If you're using the mqueue support in glibc, then you need to link against the librt library when building your code by setting LOADLIBES=-lrt in the Makefile, or adding -lrt to the compiler command line.

If you've built Michal Wronski's libmqueue library, then use -lmqueue instead of -lrt.

3. Using mqueues

From a programmer's (API) viewpoint, there is a good introduction in Unix Network Programming (Richard W Stevens) Volume 2 Chapter 5.

Example code from this book is available here.

Beware: the UNP example code may need modifying to work on linux. For example, on my machine, the configure script incorrectly added #define statements for uint8_t, uint16_t and uint32_t in the top level config.h file which caused compile errors about duplicate types in the declaration. Commenting- out these lines solved the problem, but it would be nice to fix the config script/macros to solve this properly. I also needed to add in a declaration of a uint_t type in the unpipc.h header (there's one of these in each example directory). You need to first compile the libraries for the example (do a make in the lib directory). Then you need to make the set of examples you are interested in (cd to pxmsg and run make). The book explains how to use the examples.

4. That's all, folks

If you find any problems with the above, or have any additional pertinent information to add, then please contact me at the email address on my home page.