无法设置POSIX消息队列属性

七分之九

我的环境:

  • 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;
}
食人鱼

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

mq_unlink()在您打开之前调用,并且可能还设置了O_EXCL,然后看看有什么不同。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用--ulimit选项在Docker容器中将POSIX消息队列限制设置为无限制

应该使用什么SystemV消息队列或POSIX消息队列?

System V消息队列与posix消息队列测试

使用进程内通信POSIX消息队列

linux命令检查POSIX消息队列

例外:TypeError:无法设置未定义的属性“消息”

POSIX消息队列-打开时出错:参数无效

使用POSIX消息队列的单进程线程安全

UIAlertcontroller设置属性消息

C IPC-无法从队列接收消息

C:无法打开消息队列

无法启用包含5条消息的队列

无法使用 Masstransit 读取队列消息

如何从c中的POSIX消息队列中删除或清除所有消息?

Activemq设置-无法将消息发送到队列(错误-java.io.IOException:未知数据类型:47)

如何设置 AMQ 消息的属性

我的js代码的错误消息-未捕获的TypeError:无法设置未定义的属性”

尝试使用队列外的 GET 消息(不是 JMS)设置 MQ

在Mac OSX上设置System V消息队列大小

使用 python 在 Azure 队列中设置消息的 messagettl

设置临时队列后,JMS不使用消息

在Node.js中为Azure队列消息设置TimeToLive

来自消息队列激活的过程应该设置为循环吗?

如何在不删除项目的情况下窥视Linux(POSIX)消息队列?

POSIX消息队列-重新启动后它仍然存在吗?

如何使用posix消息队列在节点和python之间进行IPC?

POSIX4消息队列“ mq_open:没有这样的文件或目录”

posix管道作为消息队列:在阻塞的写入+信号上会发生什么

si_value不是使用POSIX消息队列的sigaction处理程序中siginfo_t的成员