无法设置 POSIX 消息队列属性

2024-02-02

我的环境:

  • CentOS 6.5(64位内核)
  • 海湾合作委员会 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;
}

以下代码有效。

按照 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法设置 POSIX 消息队列属性 的相关文章

随机推荐

  • React Redux:获取 Props 并更新状态

    我是第一次尝试 React Redux JS 我对在组件中设置状态与让 redux 更新它有点困惑 我想单击一个按钮将 lightOn 设置为 true 并显示更新的 this props lightOn 值 我错过了一些基本的东西 但不确
  • JsonConverter如何反序列化为通用对象

    我通过 webapi 发送这个结构 DataContract public class PacketData public enum Opcodes Hello 0x00 Close 0x01 Serial 0x02 GPIO 0x04 D
  • 模型类未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中

    我正在使用 sphinx 并尝试为我的 Django 项目生成文档 我决定首先尝试记录模型 因此在我的 rst 文件中我这样做了 wdland models automodule wdland models members undoc me
  • 如何检测innerHTML何时完成

    我已经做了很多寻找这个问题的解决方案 但到目前为止还没有找到一个可以跨浏览器工作的解决方案 我需要的是一个原始的javascript函数 一旦innerHTML成功插入到dom中 它将接受一个元素并运行回调 e g var element
  • 对 CUBEVALUE 中的多个度量求和

    我尝试了多个不同的函数 CUBESET CUBEVALUE 等 但我似乎无法找到一种方法来在同一公式中对多个度量求和 关于如何完成这项工作有什么建议吗 我进行了大量搜索但找不到方法 想法如下 但这不起作用 CUBEVALUE Connect
  • org.webrtc.RTCPeerConnection 无法将视频发布到服务器

    我使用 webrtc 在会议中发送和获取视频 子主视频正常显示 但问题是视频没有发布到服务器 我检查方法 setlocalDescription 没有返回错误 这是我的sdp 有人可以帮忙吗 我搜索了很多解决方案 但我仍然不知道我的问题 我
  • Oracle PLSQL IN() 子句中的数组

    我将字符串数组 plcListchar 传递给存储过程 我想在 IN 子句中使用这个字符串数组 我不能在 IN 子句中直接使用 plcListchar 让我展示一下如何在 JAVA 中创建 plcListchar 字符串数组 String
  • github 操作workflow_run.conclusion 随机失败的任何解决方法吗?

    我在用着workflow run conclusion按照以下方式发送工作流程通知github 文档 https docs github com en actions using workflows events that trigger
  • Python 与电报机器人中的关键字“from”冲突

    我想使用 python telegram bot 在 Python 脚本中打印用户信息 参考this https core telegram org bots api user page 但是当我打字时 print update messa
  • 如何在 Azure 表存储中存储任意键值对?

    背景 我从客户端收到 CSV 数据文件 其中包含大量我不需要的数据和少量我需要的数据 将来我可能需要访问这些数据 虽然我正在存档原始数据文件 但我希望有一些更容易查询的东西 我希望找到一个并不意味着数据文件保持相同格式的解决方案 即客户端可
  • 在 XNA 4.0 中绘制具有多个侧面的纹理立方体

    几个小时以来我一直在努力解决这个问题 我想做的是画一个立方体 每一面都有不同的纹理 或者更具体地说 我希望能够指定每面我想要的任何纹理 我用了这个例子here http www switchonthecode com tutorials c
  • GWT:如何更改 GWT Celltable 中的行颜色

    我在 GWT 中有一个单元格表 我可以通过此更改特定列的颜色 celltable addColumnStyleName 4 bluetext 但我怎样才能改变例如第3行的颜色 Thanks 您必须提供一个RowStyles返回每行的 css
  • 什么是异步图片下载以及如何下载过多图片?

    我有太多图像无法从网络下载到 iPhone 中 如何使用异步图像下载构建应用程序 最常见和最简单的方法是使用 NSURLConnection 进行异步请求 与请求集委托创建连接 当收到下一个数据块 完成加载或失败时 它开始在后台调用委托方法
  • Netbeans 可能存在错误,它正在执行我更改的旧代码

    我一直在一个包含 500 多个类的项目中进行编码 其中一个类负责从磁盘检索数据 由于某些原因 我更改了代码 但是当我运行代码时 它仍然执行类的旧代码 当我调试时 调试器会移过空行 这意味着它仍然运行我更改的旧代码 我多次清理和构建了该项目
  • 片段 YouTubePlayerSupportFragment 中存在重复的 ID

    我正在尝试使用 API Youtube 执行播放视频 但在 XML 重复 ID 0x7f080039 中收到错误 知道如何解决这个问题吗 我的片段扩展了 YouTubePlayerSupportFragment Override publi
  • 为什么`false && true || true` 评估为 true?

    根据MDN 逻辑运算符 https developer mozilla org en US docs Web JavaScript Reference Operators Logical Operators page false 任何短路评
  • 在Django项目中,如何通过迁移文件重建模型类?

    在Django项目中 如果模型文件夹文件被删除 是否可以使用migrations文件夹文件来重建模型类 mohammadKazemSamiel 我用我的 Postgres 数据库尝试了以下操作 python manage py inspec
  • 我可以使用 Gmail API 获取 Gmail 草稿的 URL 链接吗?

    新的 Gmail API 允许我们创建和查看草稿 但是有没有办法获得 URL 链接来查看草稿呢 我可以使用草稿的 ThreadId 手动创建链接 如下所示 https mail google com mail u 0 drafts comp
  • PHP 错误处理:die() 与trigger_error() 与 throw Exception

    关于 PHP 中的错误处理 据我所知有 3 种风格 die or exit style con mysql connect localhost root password if con die Could not connect mysql
  • 无法设置 POSIX 消息队列属性

    我的环境 CentOS 6 5 64位内核 海湾合作委员会 4 4 7 20120313 我正在尝试设置 POSIX 消息队列的属性 但代码不会更改该属性 我只获得默认属性值 你能指出我的代码有什么问题吗 我以用户 而不是 root 身份执