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

5

man 7 mq_overview 表示 POSIX 标准中的消息队列可以使用通常用于文件 (例如 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);
}

在包含5个消息和549字节的msg队列/myq上运行此操作将会产生以下结果:
$ 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     

$

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

因此,文件/dev/mqueue/myq与消息队列相关联。

我的问题是:队列本身在哪里,即549个字节在哪里?我猜它们位于内核内部的某个列表类型数据结构中,但我没有在手册等文档中看到这方面的说明,想知道如何了解它。.

1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接