larsson.dev
1 mins

Posix message queues on FreeBSD 12.1

2020-05-31 / 127 Words

So I was playing around with different UNIX IPC and getting posix message queues took me quite a while to figure out. So I thought I document what I did for future reference.

TL;DR

mkdir /mnt/mqueuefs
mount -t mqueuefs none /mnt/mqueuefs

mq.c

#include <stdio.h>
#include <fcntl.h>
#include <mqueue.h>

int main( int argc, char** argv) {

    struct mq_attr attr;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 1024;
    mqd_t mid = mq_open("/test_mq", O_CREAT|O_RDWR, 0666, &attr);

    if (mid == (mqd_t)-1 ) {
        perror("mq_open");
        return 1;
    }

    if (-1 == mq_send(mid, "hello", 6, 0)) {
        perror("mq_send");
        return 1;
    }
    printf("sent %s\n", "hello");;

    char buf[1024];
    if (-1 == mq_receive(mid, buf, 1024, 0)) {
        perror("mq_send");
        return 1;
    }
    printf("recvt %s\n", buf);

    return 0;
}
$ cc -lrt -o mq mq.c
$ ./mq
sent hello
recv hello

TBC