Message Queues
Structured messaging.
Message Queues is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Structured Messaging
A message queue lets processes exchange discrete messages rather than a raw byte stream. The kernel preserves message boundaries, so each send becomes exactly one receive.
Messages vs Streams
Pipes deliver a byte stream where boundaries blur. Message queues deliver whole units:
- Each
sendis one message - Each
receivereturns one message - Messages can be prioritized
POSIX vs System V Queues
Two APIs exist:
- POSIX:
mq_open,mq_send,mq_receive, named with a slash - System V:
msgget,msgsnd,msgrcv, key-based with typed messages
POSIX is the cleaner, modern choice.
Opening a Queue
mq_open creates or opens a named queue and returns a descriptor. You can pass attributes like maximum message count and size.
#include <mqueue.h>
#include <fcntl.h>
mqd_t open_queue(const char *name) {
return mq_open(name, O_CREAT | O_RDWR, 0644, NULL);
/* name like "/myqueue"; NULL uses default attributes */
}Queue Attributes
struct mq_attr defines mq_maxmsg (capacity) and mq_msgsize (max bytes per message). The receiver's buffer must be at least mq_msgsize.
#include <mqueue.h>
void set_attr(struct mq_attr *a) {
a->mq_flags = 0;
a->mq_maxmsg = 10; /* up to 10 queued messages */
a->mq_msgsize = 256; /* each at most 256 bytes */
a->mq_curmsgs = 0;
}Sending a Message
mq_send places a message on the queue with a priority. Higher-priority messages are delivered first.
#include <mqueue.h>
#include <string.h>
int post(mqd_t q, const char *text) {
return mq_send(q, text, strlen(text) + 1, 5); /* priority 5 */
}Receiving a Message
mq_receive returns the highest-priority message. The buffer must be at least mq_msgsize or the call fails with EMSGSIZE.
#include <mqueue.h>
#include <stdio.h>
void get(mqd_t q, size_t msgsize) {
char buf[256];
unsigned prio;
ssize_t n = mq_receive(q, buf, msgsize, &prio);
if (n >= 0) printf("got (prio %u): %s\n", prio, buf);
}Priorities Order Delivery
Unlike a FIFO pipe, a message queue delivers by priority first, then arrival order within the same priority. This lets urgent messages jump ahead.
Blocking vs Non-Blocking
By default mq_send blocks when the queue is full and mq_receive blocks when empty. Open with O_NONBLOCK to make these return EAGAIN instead of waiting.
Timed Operations
mq_timedsend and mq_timedreceive accept an absolute deadline, failing with ETIMEDOUT if the operation cannot complete in time. This avoids waiting forever.
Cleanup
Close the descriptor with mq_close, and remove the named queue from the system with mq_unlink so it does not persist after all users are done.
#include <mqueue.h>
void cleanup(mqd_t q, const char *name) {
mq_close(q);
mq_unlink(name); /* remove the named queue */
}Quick Check
Test your understanding of message queues.
Recap
You learned POSIX message queues.
- Exchange discrete messages, not raw bytes
mq_open,mq_send,mq_receive- Delivery is by priority, then arrival order
- Optional non-blocking and timed variants; clean up with mq_close and mq_unlink
Frequently asked questions
Is the “Message Queues” lesson free?
Yes — the full text of “Message Queues” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Message Queues”?
Structured messaging. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Message Queues” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pipes
- Shared Memory
- Message Queues
- Signals