【问题标题】:cannot set POSIX message queue attribute无法设置 POSIX 消息队列属性
【发布时间】:2014-11-06 03:17:52
【问题描述】:

我的环境:

  • CentOS 6.5(64 位内核)
  • gcc 4.4.7 20120313

我正在尝试为 POSIX 消息队列设置属性,但代码不会更改该属性。 我只得到默认属性值。

您能指出我的代码有什么问题吗?

我以用户(不是 root)的身份执行 a.out。

#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>

/*
gcc [file] -lrt
*/

static void showAttr(mqd_t mqd)
{
    struct mq_attr attr;

    mq_getattr(mqd, &attr);

    printf("maxmsg = %d\n", attr.mq_maxmsg);
    printf("msgsize = %d\n", attr.mq_msgsize);
    printf("curmsgs = %d\n", attr.mq_curmsgs);

}

int main()
{
    mqd_t mqd;
    int flags;
    int ret;
    struct mq_attr attr;

    flags = O_RDWR | O_CREAT;

    attr.mq_flags = 0; // or O_NONBLOCK
    attr.mq_maxmsg = 60;
    attr.mq_msgsize = 120;
    attr.mq_curmsgs = 0;

    // POSIX IPC name should start with "/"
    mqd = mq_open("/mq", flags,
//      (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
        0644,
        &attr );

    if (mqd < 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");

    sleep(1);

    showAttr(mqd);

    ret = mq_close(mqd);
    if (ret != 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);     
    }
    printf("close ok\n");

    return 0;
}

我发现以下代码有效。 但是,当我尝试设置 attr.mq_maxmsg (=60) 时,mq_open 失败。

#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>

/*
gcc [file] -lrt
*/

static void showAttr(mqd_t mqd)
{
    struct mq_attr attr;

    mq_getattr(mqd, &attr);

    printf("maxmsg = %d\n", attr.mq_maxmsg);
    printf("msgsize = %d\n", attr.mq_msgsize);
    printf("curmsgs = %d\n", attr.mq_curmsgs);

}

int main()
{
    mqd_t mqd;
    int flags;
    int ret;
    struct mq_attr attr;

    flags = O_RDWR | O_CREAT;

    // POSIX IPC name should start with "/"

    // 1. once open without attribute setting
    mqd = mq_open("/mq", flags,
        (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
    mq_getattr(mqd, &attr);
    mq_unlink("/mq");
    mq_close(mqd);

    // 2. set values of attribute
    // attr.mq_maxmsg = 10;
    attr.mq_msgsize = 120;

    // 3. allocate attribute
    mqd = mq_open("/mq", flags,
        (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
    //   0644,
        &attr );

    if (mqd < 0) {
        printf("open failed %d\n", mqd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");

    sleep(1);

    showAttr(mqd);


    ret = mq_close(mqd);
    if (ret != 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);     
    }
    printf("close ok\n");

    return 0;
}

【问题讨论】:

  • 提到设置值高于允许的最大值会导致失败并指出最大值的定义位置是一个巨大的帮助。非常感谢。

标签: c linux queue posix


【解决方案1】:

以下代码有效。

我按照 pilcrow 的建议在 mq_open() 之前使用了 mq_unlink()。

而且,问题的另一个根源是我将 attr.mq_maxmsg 设置为大于 10(在 /proc/sys/fs/mqueue/msg_max 中定义)。 如果我设置 attr.mq_maxmsg 小于等于 10,这些设置属性都没有问题。

#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>

/*
gcc [file] -lrt
*/

static void showAttr(mqd_t mqd)
{
    struct mq_attr attr;

    mq_getattr(mqd, &attr);

    printf("maxmsg = %d\n", attr.mq_maxmsg);
    printf("msgsize = %d\n", attr.mq_msgsize);
    printf("curmsgs = %d\n", attr.mq_curmsgs);

}

int main()
{
    mqd_t mqd;
    int flags;
    int ret;
    struct mq_attr attr;

    flags = O_RDWR | O_CREAT;

    mq_unlink("/mq");

    attr.mq_flags = 0;
    attr.mq_maxmsg = 3; // ***
    attr.mq_msgsize = 141;
    attr.mq_curmsgs = 0;

    mqd = mq_open("/mq", flags,
        (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
        &attr );

    if (mqd < 0) {
        printf("open failed %d\n", mqd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");

    sleep(1);

    showAttr(mqd);


    ret = mq_close(mqd);
    if (ret != 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);     
    }
    printf("close ok\n");

    return 0;
}

【讨论】:

    【解决方案2】:

    mq_open() 正在返回一个已经存在的队列,其属性在创建时设置。因此,O_CREAT 标志无效,您指定的属性也会被忽略。

    在你打开之前调用mq_unlink(),也许也设置O_EXCL,看看有什么不同。

    【讨论】:

    • @pilcrow:谢谢你的回复。我在 mq_open() 之前添加了 "mq_unlink("/mq");,但是我得到 "open failed"。我也尝试了 O_EXCL,但它没有成功。
    • @sevenOfNine,什么错误?听起来像是一个不同的新问题。
    • @pilcrow: mq_open 返回 -1。看来我的问题类似于stackoverflow.com/questions/21465734/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2020-11-27
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2016-03-21
    • 2015-04-12
    相关资源
    最近更新 更多