Don Slowik
Don Slowik

Reputation: 1120

Where are POSIX message queues located (Linux)?

man 7 mq_overview says that the POSIX "...message queues on the system can be viewed and manipulated using the commands usually used for files (e.g., ls(1) and rm(1))." For example I was able to read using a mqd_t as a file descriptor as follows:

#include <iostream>
#include <fcntl.h>
#include <mqueue.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  if (argc != 2) {
    std::cout << "Usage: mqgetinfo </mq_name>\n";
    exit(1);
  }
  mqd_t mqd = mq_open(argv[1], O_RDONLY);

  struct mq_attr attr;
  mq_getattr(mqd, &attr);
  std::cout << argv[1] << " attributes:"
        << "\nflag: " << attr.mq_flags
        << "\nMax # of msgs: " << attr.mq_maxmsg
        << "\nMax msg size: " << attr.mq_msgsize
        << "\nmsgs now in queue: " << attr.mq_curmsgs << '\n';

  // Get the queue size in bytes, and any notification info:
  char buf[1024];
  int n = read(mqd, buf, 1023);
  buf[n] = '\0';
  std::cout << "\nFile /dev/mqueue" << argv[1] << ":\n"
        << buf << '\n';

  mq_close(mqd);
}

Running this on msg queue /myq when it contains 5 msgs, 549 bytes gives:

$ g++ mqgetinfo.cc  -o mqgetinfo -lrt
$ ./mqgetinfo /myq
/myq attributes:
flag: 0
Max # of msgs: 10
Max msg size: 8192
msgs now in queue: 5

File /dev/mqueue/myq:
QSIZE:549        NOTIFY:0     SIGNO:0     NOTIFY_PID:0     

$

Also:

$ !cat
cat /dev/mqueue/myq
QSIZE:549        NOTIFY:0     SIGNO:0     NOTIFY_PID:0 

So the file /dev/mqueue/myq has some info associated with the msg queue.

My question is: Where is the queue itself, i.e., where are the 549 bytes? I'm guessing they are in some list-type data structure internal to the kernel, but I don't see this mentioned in the man pages etc, and wondering how to find out about that..

Upvotes: 5

Views: 4063

Answers (1)

pah
pah

Reputation: 4778

Since the internal handling of message queues are implementation specific (not part of the standard, as it only specifies the programming interface and behaviour), I recommend you to have a look into the linux kernel source file ipc/mqueue.c and pay special attention to mqueue_create() and msg_insert() functions, as it's a good place to get started if you want to understand how message queues are implemented in the linux kernel.

Upvotes: 3

Related Questions