首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在读取模式下打开消息队列(mq_open())

无法在读取模式下打开消息队列(mq_open())
EN

Stack Overflow用户
提问于 2015-03-08 06:18:09
回答 1查看 2.3K关注 0票数 2

我有一个程序,它创建一个消息队列并向这个队列发送一条消息(用mq_send())。然后,我尝试打开相同的消息队列来读取来自另一个进程的消息。但是mq_open()返回-1。

open_and_write_MQ.c

代码语言:javascript
复制
#include <stdio.h>
#include <mqueue.h>
#include <stdlib.h>
#include <string.h>

#define LEN 50


int main(int argc, char * argv[])
{

    struct mq_attr attr;
    mqd_t fd;
    char buff[LEN];
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = LEN;
    attr.mq_curmsgs = 0;
    memset(buff,0,LEN);
    strcpy(buff,"This is just a test message");
    if(argc < 2)
    {
        printf("\nPlease enter at least one argument\n");
        exit(0);
    }   
    else
    {
        fd = mq_open(argv[1], O_CREAT | O_RDWR, 7777, &attr);  
        if(fd == -1)
        {
            printf("\nCould not create a message queue\n");
            exit(0);
        }
        else
        {
            if(mq_send(fd, buff, sizeof(buff), 1) == -1)
            {
                printf("\nCouldn't write message to the queue\n");
                mq_close(fd);
                mq_unlink(argv[1]);
                exit(0);
            } 
            else
            {
                printf("\n Message written to the queue sucussfully\n");
            }
        }
    }
    mq_close(fd);
    printf("\n waiting for other process to read the MQ\n");
    getchar();
    mq_unlink(argv[1]);
    exit(0);
}

这将创建MQ。下面的程序尝试读取相同的MQ。open_and_read_MQ.c

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <string.h>
#include <errno.h>

#define LEN 50

int main(int argc , char *argv[])
{
    mqd_t fd;
    char buff[LEN];
    int sterr;
    memset(buff,0,LEN);
    if(argc < 2)
    {
        printf("\nPlease enter the name of message queue\n");
        exit(0);
    }
    else
    {
        fd = mq_open(argv[1], O_RDONLY);
        if(fd == -1)
        {
            sterr = errno;
            printf("\nCouldn't open the message queue. Error : %s\n",strerror(sterr));
            exit(0);
        }
        else
        {
            if(mq_receive(fd, buff, sizeof(buff), NULL) == -1)
            {
                printf("\nMessage could not be received\n");
                mq_close(fd);
                exit(0);
            }   
            else
            {
                printf("Received Message : %s",buff);
            }
        }
    }
    exit(0);
}

汇编步骤:

代码语言:javascript
复制
$ gcc open_and_read_MQ.c -lrt -o open_and_read_MQ
$ gcc open_and_write_MQ.c -lrt -o open_and_write_MQ

执行步骤:

代码语言:javascript
复制
$ ./open_and_write_MQ /new
Message written to the queue sucussfully

waiting for other process to read the MQ

然后在其他终端运行下面的程序。

代码语言:javascript
复制
$ ./open_and_read_MQ /new
Couldn't open the message queue. Error : Permission denied

如何设置进程的权限,以便它可以从消息队列中读取消息?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-08 16:04:53

问题是这条线

代码语言:javascript
复制
fd = mq_open(argv[1], O_CREAT | O_RDWR, 7777, &attr);

7777不是以八进制指定的数字。使用例如0777 (或open(2)中描述的S_I*常量)。不过,这些权限可能过于宽泛。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28923694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档