使用mqueue.h创建消息队列

huangapple go评论52阅读模式
英文:

Create message queue with mqueue.h

问题

output: -1
errno: 22 (EINVAL)

英文:

I am trying to create a posix message queue using mq_open from <mqueue.h>.

MAX_MSG and MAX_MSG_LGTH are macros currently defined as 256 and name is never longer than MAX_MSG_LGTH.

This is the code I currently have:

mqd_t func(char* name)
{
    mqd_t queueId;
    char localbuffer[MAX_MSG_LGTH + 1];
    localbuffer[0] = '/';
    strncpy(&localbuffer[1], name, MAX_MSG_LGTH);
    localbuffer[MAX_MSG_LGTH] = '\0';

    struct mq_attr mqAttr = {0, MAX_MSG, MAX_MSG_LGTH, 0};

    queueId = mq_open(localbuffer, O_RDWR | O_CREAT, 007, &mqAttr);

    return queueId;
}
  • input: "task1"
  • output: -1
  • errno: 22 (EINVAL)

I have checked localbuffer and the queue name is being stored correctly ("/task1").

答案1

得分: 2

Your MSG_MAX value is [may be] too large.

我运行了你的代码并减小了MSG_MAX,最终它正常工作了。


要查看系统范围的值,你可以尝试:

head -1000 /proc/sys/fs/mqueue/*

在我的系统上,我得到了:

==> /proc/sys/fs/mqueue/msg_default <==
10

==> /proc/sys/fs/mqueue/msg_max <==
10

==> /proc/sys/fs/mqueue/msgsize_default <==
8192

==> /proc/sys/fs/mqueue/msgsize_max <==
8192

==> /proc/sys/fs/mqueue/queues_max <==
256

请注意,你可以[作为root]写入这些文件以更改/增加这些值。


此外,MSG_MAX_LGTH 与文件名的最大长度无关。

文件名的最大长度始终为256。


对于权限使用007有点混乱。它表示_其他_用户具有完全访问权限,但_你的_用户没有权限。

我认为你想要0700或[更好]0600。请注意,前导的0是需要的,以将该值解释为_八进制_。

英文:

Your MSG_MAX value is [may be] too large.

I ran your code and I reduced MSG_MAX and [eventually] it worked.


To see the system-wide values you can use, try:

head -1000 /proc/sys/fs/mqueue/*

On my system, I got:

==> /proc/sys/fs/mqueue/msg_default <==
10

==> /proc/sys/fs/mqueue/msg_max <==
10

==> /proc/sys/fs/mqueue/msgsize_default <==
8192

==> /proc/sys/fs/mqueue/msgsize_max <==
8192

==> /proc/sys/fs/mqueue/queues_max <==
256

Note that you can [as root] write to these files to change/increase the values.


Also, MSG_MAX_LGTH has nothing to do with the max size for the filename.

The max length for the filename is always 256.


Using 007 for a permission is a little screwy. It says that other users have full access, but your user has none.

I think you want 0700 or [better] 0600. Note that the leading 0 is needed to interpret the value as octal.

huangapple
  • 本文由 发表于 2023年2月9日 01:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定