如下代码:
#include
#include
#include
#include
#include
#include
#include
#include
#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;
}
但是不对劲,相关的mq函数报错:undefined reference
man 一下发现,注意红色字体:
---------------------------------------------------------------------------------------------------------------------------------------------
MQ_OPEN(3) Linux Programmer's Manual MQ_OPEN(3)
NAME
mq_open - open a message queue
SYNOPSIS
#include
#include
#include
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
#include
#include
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" 。