2016-07-22 69 views
3

man 7 mq_overview表示可以使用通常用于文件的命令(例如,ls(1)和rm(1))来查看和操纵系统上的POSIX“...消息队列”。 例如我能够使用mqd_t作为一个文件进行读取如下:POSIX消息队列位于哪里(Linux)?

#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); 
} 

上MSG队列/ 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  

$ 

另外:

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

所以文件/ dev/mqueue/myq有一些与msg队列相关的信息。

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

回答

1

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