Posix 消息队列的创建问题

在创建Posix消息队列时遇到链接错误和权限问题。需在编译时添加'-lrt'选项,如:gcc -o test test.c -lrt。创建失败的错误提示为'errno:13, Permission denied',原因是name参数应遵循'/somename'格式,不需要实际文件存在。解决方案是将宏MQ_DATA_FILE改为'/tmp_mq_ipc_file'。" 120496486,11222865,数据库连接池的创建与管理,"['数据库', '连接池', 'Java', '数据库管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如下代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <mqueue.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#define MQ_DATA_FILE "/tmp/tmp_mq_ipc_file"

int main(int argc, char **argv)
{
    mqd_t mq_list;
    struct mq_attr qu_attr;
    int flags = O_RDWR | O_NONBLOCK | O_CREAT | O_EXCL;
    mode_t mode  = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

    mq_list = mq_open(MQ_DATA_FILE, flags, mode, NULL);
    if (mq_list == -1)
    {
        printf("create mq failed, errno:%d,%s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    printf("MQ creat succeed!\n");
    memset(&qu_attr, 0, sizeof(struct mq_attr));
    if (mq_getattr(mq_list, &qu_attr) == -1)
    {
        printf("get mqueue attr failed,errno:%d,%s\n", errno, strerror(errno));
        //exit(EXIT_FAILURE);
    }
    else
    {
        printf("mq_attr.mq_flags:%ld\n", qu_attr.mq_flags);
        printf("mq_attr.mq_maxmsg:%ld\n", qu_attr.mq_maxmsg);
        printf("mq_attr.mq_msgsize:%ld\n", qu_attr.mq_msgsize);
        printf("mq_attr.mq_curmsgs:%ld\n", qu_attr.mq_curmsgs);
    }

    mq_close(mq_list);
    mq_unlink(MQ_DATA_FILE);

    return EXIT_SUCCESS;
}


首先,要说的是你可能要用:gcc -o test test.c 来编译这个小程序

但是不对劲,相关的mq函数报错:undefined reference

man 一下发现,注意红色字体:

---------------------------------------------------------------------------------------------------------------------------------------------

MQ_OPEN(3)                             Linux Programmer's Manual                             MQ_OPEN(3)


NAME
       mq_open - open a message queue


SYNOPSIS
       #include <fcntl.h>           /* For O_* constants */
       #include <sys/stat.h>        /* For mode constants */
       #include <mqueue.h>


       mqd_t mq_open(const char *name, int oflag);
       mqd_t mq_open(const char *name, int oflag, mode_t mode,
                     struct mq_attr *attr);


       Link with -lrt.
-----------------------------------------------------------------------------------------------------------------------------------------------

 是的,在编译的过程中要加上“-lrt”,所以编译的全部命令应该是:gcc -o test test.c -lrt


其次,打印了创建错误的log啊~!

create mq failed, errno:13,Permission denied

为什么?查了一会发现(注意红色字体):

-----------------------------------------------------------------------------------------------------------------------------------------------

MQ_OPEN(3)                             Linux Programmer's Manual                             MQ_OPEN(3)


NAME
       mq_open - open a message queue


SYNOPSIS
       #include <fcntl.h>           /* For O_* constants */
       #include <sys/stat.h>        /* For mode constants */
       #include <mqueue.h>


       mqd_t mq_open(const char *name, int oflag);
       mqd_t mq_open(const char *name, int oflag, mode_t mode,
                     struct mq_attr *attr);


       Link with -lrt.


DESCRIPTION
       mq_open() creates a new POSIX message queue or opens an existing queue.  The queue is identified
       by name.  For details of the construction of name, see mq_overview(7).

---------------------------------------------------------------------------------------------------------------------------------------------------

再次,man mq_overview,这应该是一个对posix消息队列的整体介绍,摘录其中一部分:

DESCRIPTION
       POSIX message queues allow processes to exchange data in the form of messages.  This API is dis‐
       tinct from that provided by System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.), but
       provides similar functionality.


       Message  queues  are  created and opened using mq_open(3); this function returns a message queue
       descriptor (mqd_t), which is used to refer to the open message queue in later calls.  Each  mes‐
       sage  queue  is identified by a name of the form /somename; that is, a null-terminated string of
       up to NAME_MAX (i.e., 255) characters consisting of an initial slash, followed by  one  or  more
       characters,  none  of which are slashes.  
Two processes can operate on the same queue by passing
       the same name to mq_open(3).

以上红色标记的文字,这个name参数应该是个非真实存在的文件,不需要创建,而且具有特定的格式:

一个以null结尾的字符串到NAME_MAX(即255)字符组成的初始斜杠,加上一个或更多字符,其中没有一个斜杠。

所以要想上面的程序执行成功,只需要将宏改      #define MQ_DATA_FILE "/tmp_mq_ipc_file"  。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值