2015-02-11 101 views
1

当我使用检查消息队列的QSIZE“猫的/ dev/mqueue中/ myQueue中”前后做则mq_send()/则mq_receive(后)似乎被留在队列中一些残余的字节在mq_recieve()之后。我的小的测试程序是如下:POSIX消息队列QSIZE查询

#include <stdio.h> 
#include <stdlib.h> 

#include <mqueue.h> 


#define MSG_LEN 8192 // As per mq_msgsize as returned by mq_getattr() on CentOS 7 x86_64 


int main(void) 
{ 
    mqd_t mq; 


    /* Create a message queue */ 
    if ((mq = mq_open("/myqueue", O_RDWR|O_NONBLOCK|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, NULL)) == -1) { 
     perror("Error calling mq_open()"); 
     exit(EXIT_FAILURE); 
    } 


    /* Create a message */ 

    char msg[MSG_LEN] = {" "}; 

    int msglen = snprintf(msg, MSG_LEN, "Hello, World!"); // msglen is 13 without the terminating '\0' 

    if (msglen < 0) { 
     fprintf(stderr, "Error calling snprintf()\n"); 
     exit(EXIT_FAILURE); 
    } 


    /* 
    * QSIZE here using "cat /dev/mqueue/myqueue" is: 0 
    * OK 
    */ 


    /* Send the message */ 

    if (mq_send(mq, msg, msglen + 1, 1) == -1) { // add 1 to msglen for the termining '\0' 
     perror("Error calling mq_send()"); 
     exit(EXIT_FAILURE); 
    } 


    /* 
    * QSIZE here using "cat /dev/mqueue/myqueue" is: 62 
    * Why not 14? 
    */ 


    /* Receive the message */ 

    if (mq_receive(mq, msg, MSG_LEN, NULL) == -1) { 
     perror("Error calling mq_receive()"); 
     exit(EXIT_FAILURE); 
    } 


    /* 
    * QSIZE here using "cat /dev/mqueue/myqueue" is: 48 
    * Why not 0? 
    */ 


    exit(EXIT_SUCCESS); 
} 

我不理解为什么62个字节被初始放置到队列中,和48个字节的残余被发送和接收14字节的消息后留下的。任何帮助将非常感激。

亲切的问候

约翰·达菲

回答

1

相信消息队列存储文件中的一些记录信息。从mq_overview()手册页:

目录中的每个文件的内容由包含有关队列信息的单一线 的:

 $ cat /dev/mqueue/mymq 
     QSIZE:129  NOTIFY:2 SIGNO:0 NOTIFY_PID:8260 

http://man7.org/linux/man-pages/man7/mq_overview.7.html