Reputation: 103
When I examine the QSIZE of a message queue using "cat /dev/mqueue/myqueue" before and after doing a mq_send()/mq_receive() there appears to be some residual bytes left on the queue after the mq_recieve(). My little test program is below:
#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#define MSG_LEN 8192 // As per mq_msgsize as returned by mq_getattr() on CentOS 7 x86_64
int main(void)
{
mqd_t mq;
/* Create a message queue */
if ((mq = mq_open("/myqueue", O_RDWR|O_NONBLOCK|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, NULL)) == -1) {
perror("Error calling mq_open()");
exit(EXIT_FAILURE);
}
/* Create a message */
char msg[MSG_LEN] = {" "};
int msglen = snprintf(msg, MSG_LEN, "Hello, World!"); // msglen is 13 without the terminating '\0'
if (msglen < 0) {
fprintf(stderr, "Error calling snprintf()\n");
exit(EXIT_FAILURE);
}
/*
* QSIZE here using "cat /dev/mqueue/myqueue" is: 0
* OK
*/
/* Send the message */
if (mq_send(mq, msg, msglen + 1, 1) == -1) { // add 1 to msglen for the termining '\0'
perror("Error calling mq_send()");
exit(EXIT_FAILURE);
}
/*
* QSIZE here using "cat /dev/mqueue/myqueue" is: 62
* Why not 14?
*/
/* Receive the message */
if (mq_receive(mq, msg, MSG_LEN, NULL) == -1) {
perror("Error calling mq_receive()");
exit(EXIT_FAILURE);
}
/*
* QSIZE here using "cat /dev/mqueue/myqueue" is: 48
* Why not 0?
*/
exit(EXIT_SUCCESS);
}
I don't understand why 62 bytes are initially put onto the queue, and a residual of 48 bytes is left behind after sending and receiving a 14 byte message. Any help would be much appreciated.
Kind regards
John Duffy
Upvotes: 1
Views: 941
Reputation: 311
I believe the message queue stores some bookkeeping information within the file. From the mq_overview()
man page:
The contents of each file in the directory consist of a single line containing information about the queue:
$ cat /dev/mqueue/mymq QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
http://man7.org/linux/man-pages/man7/mq_overview.7.html
Upvotes: 1