Skip to content

Day57 - POSIX Message Queue IPC

Goal

Learn Linux POSIX message queue IPC.

Topics:

  • blocking queue
  • non-blocking queue
  • message priority
  • mq_notify()
  • epoll integration
  • timerfd/signalfd integration

Environment

Build

make

Queue Object

Queue name:

#define DEMO_MQ_NAME "/demo_mq"

Experiment 1 - Basic Blocking Queue

Terminal 1

./receiver_notify

Terminal 2

./sender

Expected:

receiver blocks until sender sends message

Experiment 2 - Priority Queue

Sender

Send mixed priorities:

prio=1
prio=5
prio=10

Receiver

Observe receive order.

Expected:

higher priority first
FIFO inside same priority

Experiment 3 - Queue Full Blocking

Configure:

mq_maxmsg = 8

Without Receiver

Run sender repeatedly.

Expected:

mq_send() blocks when queue becomes full

Start Receiver

Receiver drains queue.

Expected:

blocked sender wakes up automatically

Experiment 4 - Non-Blocking Sender

Open sender queue with:

O_NONBLOCK

Expected:

mq_send() -> EAGAIN

instead of blocking.


Experiment 5 - Non-Blocking Receiver

Open receiver queue with:

O_NONBLOCK

Expected:

mq_receive() -> EAGAIN

when queue is empty.


Experiment 6 - mq_notify()

Receiver registers:

mq_notify()

using:

SIGEV_SIGNAL

Expected:

signal notification when queue becomes non-empty

Experiment 7 - epoll + mqueue

Receiver monitors:

  • mqueue fd

using:

epoll_wait()

Expected:

EPOLLIN when queue becomes readable

Experiment 8 - timerfd Integration

Receiver integrates:

  • timerfd

into epoll loop.

Expected:

periodic queue statistics

Experiment 9 - signalfd Integration

Receiver integrates:

  • signalfd

into epoll loop.

Signals:

  • SIGINT
  • SIGTERM

become epoll events.

Expected:

clean shutdown without signal handler

Final Architecture

epoll
 ├── mqueue
 ├── timerfd
 └── signalfd

This demonstrates a classic Linux userspace daemon event loop.