简体   繁体   English

POSIX消息队列位于何处(Linux)?

[英]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 (eg, ls(1) and rm(1))." man 7 mq_overview说可以使用通常用于文件的命令(例如,ls(1)和rm(1))查看和操作系统上的POSIX“...消息队列”。 For example I was able to read using a mqd_t as a file descriptor as follows: 例如,我能够使用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);
}

Running this on msg queue /myq when it contains 5 msgs, 549 bytes gives: 在包含5个msgs的msg队列/ myq上运行此命令,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     

$

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. 所以文件/ dev / mqueue / myq有一些与msg队列相关的信息。

My question is: Where is the queue itself, ie, where are the 549 bytes? 我的问题是:队列本身在哪里,即549字节在哪里? 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.. 我猜他们是在内核内部的一些列表类型的数据结构中,但是我没有在手册页中看到这一点,并且想知道如何找到它...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM