【问题标题】:Where are POSIX message queues located (Linux)?POSIX 消息队列位于哪里(Linux)?
【发布时间】:2016-11-26 17:40:35
【问题描述】:

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 字节时,在消息队列 /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 有一些与 msg 队列相关的信息。

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

【问题讨论】:

    标签: linux posix message-queue


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 2013-04-16
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      相关资源
      最近更新 更多