FreeBSD不会在forks上保留POSIX消息队列描述

时间:2013-10-21 16:43:46

标签: c linux message-queue freebsd

我有一个测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <mqueue.h>
#include <errno.h>
#include <fcntl.h>

int main() {
  struct mq_attr attrs;
  attrs.mq_maxmsg = 10;
  attrs.mq_msgsize = sizeof(int);

  const char name[] = "/test-queue";

  mqd_t q = mq_open(name, O_CREAT | O_RDWR, 0600, &attrs);
  if (q == (mqd_t)-1) {
    perror("mq_open");
    exit(EXIT_FAILURE);
  }

  mq_unlink(name); // it doesn't matter if I do this at the end or not

  if (fork()) {
    int msg = 666;
    if (mq_send(q, (const char *)&msg, sizeof(msg), 1)) {
      perror("mq_send");
      exit(EXIT_FAILURE);
    }
  } else {
    int msg;
    unsigned priority;
    if (mq_receive(q, (char *)&msg, sizeof(msg), &priority) == -1) {
      perror("mq_receive");
      exit(EXIT_FAILURE);
    }
    printf("%d\n", msg);
  }

  mq_close(q);

  return 0;
}

我在两个平台上使用gcc -std=c99 -Wall -o mqtest mqtest.c -lrt编译此程序:

  • Linux kallikanzarid-desktop 3.8.0-31-generic#46-Ubuntu SMP Tue Sep 10 20:03:44 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux
  • FreeBSD bsd.localhost 9.2-RELEASE FreeBSD 9.2-RELEASE#0 r255898:Thu Sep 26 22:50:31 UTC 2013 root@bake.isc.freebsd.org:/ usr / obj / usr / src / sys / GENERIC AMD64

在Linux上,一切正常。在FreeBSD上,我得到mq_receive: Bad file descriptor。将mq_unlink电话移至main()的结尾无济于事。有没有办法解决这个问题,或者我是否必须推迟标记队列以进行删除并在fork之后重新打开它?

1 个答案:

答案 0 :(得分:0)

FreeBSD 保留消息队列描述符。见mq_open(2):

  

FreeBSD基于文件描述符实现消息队列。描述符在fork(2)之后由child继承。在exec(3)之后,描述符在新图像中关闭。消息队列描述符支持select(2)和kevent(2)系统调用。

修改

mqd_t指向的结构包含描述符。但是,如果您在使用fork()的{​​{1}}之后测试该文件描述符,它也会返回EBADF。

这是FreeBSD中的一个错误。但是,在文档或实现中,我不能说错误。