POSIX消息队列mq_open问题

遇到 mq_open: Permission denied

请看:

Mounting the message queue file system
       On Linux, message queues are created in a virtual file system.   (Other
       implementations  may  also  provide such a feature, but the details are
       likely to differ.)  This file system can be mounted (by the  superuser)
       using the following commands:


           # mkdir /dev/mqueue
           # mount -t mqueue none /dev/mqueue

执行上面两条命令OK,

还是遇到同样的问题:

再看

Each message queue  is  identi fied by a name of the form /somename.  Two processes can operate on the
same queue by passing the same name to mq_open().

也就是说,消息的队列的命名方式为/mq_name

其它命名方式都不对,像/tmp/mq.2342是不对的。

OK,以下为测试代码:

/*
 ============================================================================
 Name        : mq_creat.c
 ============================================================================
 */
#include <stdio.h>
#include <mqueue.h>
#include <unistd.h>
#include <sys/stat.h>

#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

int main(int argc, char** argv) {
	mqd_t mqd;
	int c, flags;

	flags = O_RDWR | O_CREAT;
	while ((c = getopt(argc, argv, "e")) != -1) {
		switch (c) {
		case 'e':
			flags |= O_EXCL;
			break;
		}
	}

	if (optind != argc - 1) {
		printf("Invalid arg\n");
		return 1;
	}

	mqd = mq_open(argv[optind], flags, FILE_MODE, NULL);
	if (mqd == -1) {
		perror("mq_open");
		return 2;
	}
	mq_close(mqd);

	return 0;
}

不懂一定要看man pages, 

如:man 7 mq_overview


你可能感兴趣的:(POSIX消息队列mq_open问题)