【发布时间】:2016-03-04 18:30:39
【问题描述】:
我目前在 Mac OSX 上使用 System V 消息队列,并且无法将队列大小设置为大于 2048 字节的值。这是一个可编译的示例test.c:
#include <stdio.h>
#include <sys/msg.h>
#include <stdlib.h>
int main() {
// get a message queue id
int id = msgget(IPC_PRIVATE,IPC_CREAT|0600);
if (-1 == id)
exit(1);
// get message queue data structure
struct msqid_ds buf;
if (-1 == msgctl(id, IPC_STAT, &buf))
exit(1);
printf("size is %lu bytes\n", buf.msg_qbytes);
// set new buffer size
buf.msg_qbytes = 2750;
printf("setting size to %lu bytes\n", buf.msg_qbytes);
if (-1 == msgctl(id, IPC_SET, &buf))
exit(1);
// check updated message queue data structure
if (-1 == msgctl(id, IPC_STAT, &buf))
exit(1);
printf("size is %lu bytes\n", buf.msg_qbytes);
}
编译:
clang -Wall -pedantic -o test test.c
然后运行:
sudo ./test
注意:您已使用sudo 运行上述代码,以确保msgcntl 调用成功。
这个程序sn-p的输出是:
size is 2048 bytes
setting size to 2750 bytes
size is 2048 bytes
为什么队列大小没有改变?
编辑:
ipcs -Q 的输出显示:
IPC status from <running system> as of Tue Dec 1 10:06:39 PST 2015
msginfo:
msgmax: 16384 (max characters in a message)
msgmni: 40 (# of message queues)
msgmnb: 2048 (max characters in a message queue)
msgtql: 40 (max # of messages in system)
msgssz: 8 (size of a message segment)
msgseg: 2048 (# of message segments in system)
msgmnb 可以变大,还是我被卡住了?
【问题讨论】:
-
尝试运行
ipcs -Q看看是否有最大尺寸。 -
@MarkSetchell - 我用
ipcs -Q的输出更新了问题。 -
我从来没有在 OSX 上尝试过这个,我不知道它是否有效或可能会导致问题,但我猜你会做类似
sysctl -w kernel.msgmnb=2000000 -
@MarkSetchell - 是的。不幸的是,
sysctl -A | grep msgmnb什么也没返回。我没有看到任何与 System V 消息队列相关的内容:( -
@user3629249 - 这是 Mac OSX 的问题。 System V 消息队列在 Linux 上运行良好。
标签: c++ c macos ipc message-queue