POSIX 消息队列位于哪里(Linux)?

Don*_*wik 5 linux posix message-queue

man 7 mq_overviewPOSIX 表示“...系统上的消息队列可以使用通常用于文件的命令(例如 ls(1) 和 rm(1))来查看和操作。” 例如,我能够使用 mqd_t 作为文件描述符进行读取,如下所示:

#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);
}
Run Code Online (Sandbox Code Playgroud)

当消息队列 /myq 包含 5 个消息、549 字节时,在它上运行此命令会给出:

$ 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     

$
Run Code Online (Sandbox Code Playgroud)

还:

$ !cat
cat /dev/mqueue/myq
QSIZE:549        NOTIFY:0     SIGNO:0     NOTIFY_PID:0 
Run Code Online (Sandbox Code Playgroud)

因此文件 /dev/mqueue/myq 有一些与消息队列相关的信息。

我的问题是:队列本身在哪里,即549字节在哪里?我猜测它们位于内核内部的某些列表类型数据结构中,但我没有在手册页等中看到这一点,并且想知道如何找到它。

pah*_*pah 3

由于消息队列的内部处理是特定于实现的(不是标准的一部分,因为它只指定了编程接口和行为),我建议您查看 Linux 内核源文件ipc/mqueue.c并特别注意mqueue_create()msg_insert()函数,如下所示如果您想了解消息队列在 Linux 内核中是如何实现的,那么这是一个很好的起点。