Message Queues
Kurt Wall
kwall
Sat Feb 4 07:56:08 PST 2006
On Fri, Feb 03, 2006 at 09:49:18PM -0500, Kurt Wall took 41 lines to write:
> On Wed, Feb 01, 2006 at 10:44:59PM -0700, Steve Jardine took 17 lines to write:
> > Heya All,
> >
> > I have a need to send a large amount of messages over a message queue.
> > Currently, I max out at 400 entries on a specific queue. Does anyone have an idea
> > what system variable I can change to increase the number of entries on a single
> > queue?
>
> Steve:
>
> I believe the maximum number of messages is limited by the size of each
> message.
>
> $ sysctl -a | grep msg
> ...
> kernel.msgmnb = 16384 /* default size of a message queue */
> kernel.msgmni = 16 /* maximum number of message queue ids */
> kernel.msgmax = 8192 /* maximum size of a message */
>
> So, if you have a message that is 4096 bytes, you can only get 4 of
> them onto a queue. Happily, kernel.msgmnb <= INT_MAX, which is
> 2147483647 on my system.
>
> So, I believe what you need to do is increase kernel.msgmnb using
> sysctl. For example, you can say (as root):
>
> # sysctl kernel.msgmnb=100000
>
> This increases the message queue size to 100,000 bytes. You should then
> be able to stuff more messages into a single queue. I haven't tested
> this, of course. I'm doing that now.
I've established that this works as stated. The attached program
creates a message queue and adds messages to until msgsnd() fails.
The larger one makes kernel.msgmnb, the more messages it can add:
$ sysctl kernel.msgmnb
kernel.msgmnb = 16384
$ ./a.out
queue ID: 262146
message size is 17 bytes
strlen(buf.mtext) = 18
added 963 17-byte messages (16371 bytes)
$ sudo sysctl kernel.msgmnb=40000
kernel.msgmnb = 40000
$ ./a.out
queue ID: 294915
message size is 17 bytes
strlen(buf.mtext) = 19
added 2352 17-byte messages (39984 bytes)
$
Kurt
--
"If God lived on Earth, people would knock out all His windows."
-- Yiddish saying
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MSG_TYPE 1
int main()
{
int id, cnt, size, ret;
struct msgbuf {
long mtype;
char *mtext;
} buf;
/* create a queue */
if ((id = msgget(IPC_PRIVATE, 0666)) < 0) {
perror("msgget");
exit(EXIT_FAILURE);
}
printf("queue ID: %d\n", id);
/* create a message */
size = strlen("message number ") + 2;
printf("message size is %d bytes\n", size);
if ((buf.mtext = malloc(size)) == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/* initialize the message buffer */
buf.mtype = MSG_TYPE;
cnt = 0;
sprintf(buf.mtext, "message number %d", cnt + 1);
/* add messages to the queue until it fails */
while ((ret = msgsnd(id, &buf, size, IPC_NOWAIT)) == 0) {
cnt++;
sprintf(buf.mtext, "message number %d", cnt + 1);
}
printf("strlen(buf.mtext) = %d\n", (int)strlen(buf.mtext));
printf("added %d %d-byte messages (%d bytes)\n", cnt, size, cnt*size);
/* clean up */
free(buf.mtext);
return 0;
}
More information about the Linux-users
mailing list