Reputation: 2853
When i trying to reduce the number of message in POSIX message queue, its maintaining maximum of 10. Is it possible to reduce or increase the number of messages in POSIX message queue?
following code send the message to POSIX message queue. I set maximum message(MQ_MAX_NUM_OF_MESSAGES) to 5 but it sending 10 messages
send.c
#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define MSGQOBJ_NAME "/myqueue123"
#define MAX_MSG_LEN 70
#define MQ_MESSAGE_MAX_LENGTH 70
#define MQ_MAX_NUM_OF_MESSAGES 5
struct mq_attr attr;
int main(int argc, char *argv[])
{
mqd_t msgq_id;
unsigned int msgprio = 0;
pid_t my_pid = getpid();
char msgcontent[MAX_MSG_LEN];
int create_queue = 0;
char ch; /* for getopt() */
time_t currtime;
attr.mq_flags = 0;
attr.mq_maxmsg = MQ_MAX_NUM_OF_MESSAGES;
attr.mq_msgsize = MQ_MESSAGE_MAX_LENGTH;
attr.mq_curmsgs = 0;
while ((ch = getopt(argc, argv, "qp:")) != -1) {
switch (ch) {
case 'q': /* create the queue */
create_queue = 1;
break;
case 'p': /* specify client id */
msgprio = (unsigned int)strtol(optarg, (char **)NULL, 10);
printf("I (%d) will use priority %d\n", my_pid, msgprio);
break;
default:
printf("Usage: %s [-q] -p msg_prio\n", argv[0]);
exit(1);
}
}
if (msgprio == 0) {
printf("Usage: %s [-q] -p msg_prio\n", argv[0]);
exit(1);
}
if (create_queue) {
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);
} else {
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
}
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}
currtime = time(NULL);
snprintf(msgcontent, MAX_MSG_LEN, "Hello from process %u (at %s).", my_pid, ctime(&currtime));
mq_send(msgq_id, msgcontent, strlen(msgcontent)+1, msgprio);
mq_close(msgq_id);
return 0;
}
Upvotes: 2
Views: 11610
Reputation: 7210
In the linux manual page for posix message queue, you can read how to tune the related kernel configuration via the /proc filesystem.
In particular /proc/sys/fs/mqueue/msg_max is what you are looking for:
This file can be used to view and change the ceiling value for the maximum number of messages in a queue. This value acts as a ceiling on the attr->mq_maxmsg argument given to mq_open(3). The default value for msg_max is 10. The minimum value is 1 (10 in kernels before 2.6.28). The upper limit is HARD_MAX: (131072 / sizeof(void *)) (32768 on Linux/86). This limit is ignored for privileged processes (CAP_SYS_RESOURCE), but the HARD_MAX ceiling is nevertheless imposed.
edit
As explained in the mq_open(2), mq_getattr(3) and mq_send(3) manual pages, the mq_maxmsg value sets the maximum number of message that the queue can handle without blocking (or returning EAGAIN in case of non-blocking queues). If you mq_open a queue with mq_maxmsg = 5 and the kernel configuration in /proc is 10 (the default, AFAIK) than the queue will accept 5 message without blocking. If you mq_open a queue with mq_maxmsg = 15 and the kernel configuration in /proc is 10, the queue will accept 10 message. That is: you can create a queue that hold a max number of message that is lower than the maximum number of messages that the kernel accepts, but not greater.
Upvotes: 8