Adrian's answer is correct, but since this is a very common mistake that Linux has to deal with the first attempt to use POSIX message queues for something non-trivial, I thought I would add some useful information.
First, to understand the resource limit of RLIMIT_MSGQUEUE , see the formula in man setrlimit :
RLIMIT_MSGQUEUE (since Linux 2.6.8) Specifies a limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process. This limit applies to mq_open (3). Each message queue that the user creates counts (until deleted) against this limit in accordance with the formula:
bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) + attr.mq_maxmsg * attr.mq_msgsize
where attr is the mq_attr structure specified as the fourth argument to mq_open (3). The first addition in the formula, which includes sizeof (struct msg_msg *) (4 bytes on Linux / i386), ensures that the user cannot create an unlimited number of messages with zero length (such messages, however, each of them consumes some system memory for overhead).
Given the default MQ settings ( mq_maxmsg = 10, mq_msgsize = 8192) on Linux, the above formula only works for 10 message queues for a default limit of 819,200 bytes. Therefore, why do you encounter this problem as soon as you, for example. forget to close and disconnect a couple of queues after they are completed.
To increase the resource limit RLIMIT_MSGQUEUE to the maximum permissible for the user user, you can use something like the following in your application startup code:
#ifdef __linux__
If you also set the mq_maxmsg and mq_msgsize to lower values ββwhen you open the queue (see man mq_open ), you can leave with several hundred queues even within the hard limit by default of RLIMIT_MSGQUEUE . Depending on your specific use case, of course.
Setting the hard limit RLIMIT_MSGQUEUE not difficult if you have root access to the system. After you figure out what the limit should be, configure the system-wide settings in /etc/security/limits.conf . For example, to set the hard and soft limit of 4 megabytes for the www-data user group and no restrictions for the superuser, you must add the following lines to the file:
@www-data - msgqueue 4194304 root - msgqueue unlimited