【发布时间】: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