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

din*_*elk 11 c c++ macos ipc message-queue

我目前在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);
}
Run Code Online (Sandbox Code Playgroud)

编译:

clang -Wall -pedantic -o test test.c
Run Code Online (Sandbox Code Playgroud)

并运行:

sudo ./test
Run Code Online (Sandbox Code Playgroud)

注意:您已运行上述代码sudo以确保msgcntl调用成功.

该程序片段的输出是:

size is 2048 bytes
setting size to 2750 bytes
size is 2048 bytes
Run Code Online (Sandbox Code Playgroud)

为什么队列大小没有改变?

编辑: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)
Run Code Online (Sandbox Code Playgroud)

可以msgmnb做得更大,还是我卡住了?

ter*_*ill 6

似乎OS X不允许增加消息队列大小.系统V实现很旧,根本没有记录.我还发现奇怪的是,在message.h中缺少定义MSGMNB,MSGMAX,而你可以在Linux和其他Unix实现中找到它.

我也发现了这个:

OS X是最糟糕的.每个队列限制为2048个字节,OS X默默地忽略尝试增加它(就像FreeBSD一样).为了增加对伤害的侮辱,似乎没有办法在重新编译内核之前增加这个限制.我猜这是基于Darwin消息队列限制.(http://semanchuk.com/philip/sysv_ipc/)

该文件于2014年9月更新,并确认了苹果邮件列表中的帖子:

http://lists.apple.com/archives/unix-porting/2008/Jan/msg00033.html

@Mark Setchell在评论中指出.

此外,OS X不支持最近的Ruby Wrapper实现,因为作者声明:

消息由计算机内核处理.并非所有内核都支持POSIX消息队列,一个值得注意的例子是Darwin(OS X).Darwin实现了较旧的System V IPC API.(https://github.com/Sirupsen/posix-mqueue)

在Web上有其他来源(大多数是旧的),表明除了重新编译内核以增加消息队列限制之外别无他法.

UPDATE:苹果的地位是不鼓励使用系统V IPC的位置:

支持一些System V原语,但不鼓励使用它们以支持POSIX等价物.

侧面建议,添加: msgctl(id, IPC_RMID, NULL); 在测试代​​码的末尾,有人(像我一样,感叹!)可能会忘记每个队列必须关闭.