Posix 消息队列的创建问题
2012-07-18 15:05
405 查看
如下代码:
首先,要说的是你可能要用: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" 。
#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" 。
相关文章推荐
- Posix消息队列的基本操作——创建或打开
- POSIX消息队列mq_open问题
- POSIX 消息队列相关问题
- linux下Posix消息队列的创建和删除
- POSIX 消息队列相关问题
- POSIX 消息队列相关问题
- 【学习随手记】POSIX消息队列执行报Permission denied的问题。
- POSIX和SYSTEM的消息队列应该注意的问题
- POSIX和SYSTEM的消息队列应该注意的问题
- IPC通信:Posix消息队列的创建,关闭,删除
- 关于POSIX消息队列mq_open提示错误信息:Invalid argument的问题
- Posix消息队列的基本操作——创建或打开
- POSIX和SYSTEM的消息队列应该注意的有关问题
- (UNP点滴记录) Posix消息队列
- android broadcastreceiver 重复创建和无法接受消息问题
- Linux IPC实践(7) --Posix消息队列
- Unix IPC之Posix消息队列(2)
- 使用Lock与ReentrantLock模拟消息队列阻塞,生产与消费问题模拟
- MQ资源管理器队列中消息中文乱码问题
- 分布式消息队列RocketMQ&Kafka -- 消息的“顺序消费”-- 一个看似简单的复杂问题