CodeBlanks Logo Icon CodeBlanks

Message queue

Learn message queues with msgget and msgsnd.

Message queue

A message queue is an IPC structure where processes send and receive typed messages in order. It is created with msgget(key, flags); the IPC_PRIVATE key produces a new queue. Each message has a long mtype field; the receiver uses it as a filter.

The message structure is user-defined, e.g. struct Msg { long mtype; char mtext[16]; };. It is sent with msgsnd(msqid, &msg, sizeof(msg.mtext), 0) and received with msgrcv(msqid, &msg, sizeof(msg.mtext), 1, 0). mtype = 1 targets; 0 accepts any type. msgctl(msqid, IPC_RMID, NULL) removes the queue.

Message struct and flow

The first field of the message struct must always be long mtype; otherwise msgsnd and msgrcv misbehave. The mtext field is optional; you pass its size explicitly to the functions.

With fork the child can be the receiver and the parent the sender. The child waits on msgrcv, which is a blocking call. When the parent calls msgsnd the child wakes up, takes the message and prints it. wait(NULL) lets the parent reap the child, then msgctl(IPC_RMID) removes the queue.

Practice Send data via message queue

Define the struct struct Msg { long mtype; char mtext[16]; };. Set up a queue with int msqid = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);. Fork a child with pid_t pid = fork();. In the child: struct Msg rmsg; msgrcv(msqid, &rmsg, sizeof(rmsg.mtext), 1, 0);, printf("%s\n", rmsg.mtext);, _exit(0);. In the parent: struct Msg msg; msg.mtype = 1; strncpy(msg.mtext, "merhaba mq", sizeof(msg.mtext) - 1);, msgsnd(msqid, &msg, sizeof(msg.mtext), 0);, wait(NULL);, msgctl(msqid, IPC_RMID, NULL);.

Starter code

main.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

// Define message struct
// Set up queue and fork child receiver
// Parent sends message and cleans up

int main(void) {
    return 0;
}

Sample solution

main.c
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

struct Msg { long mtype; char mtext[16]; };

int main(void) {
    int msqid = msgget(IPC_PRIVATE, 0600 | IPC_CREAT);
    pid_t pid = fork();
    if (pid == 0) {
        struct Msg rmsg;
        msgrcv(msqid, &rmsg, sizeof(rmsg.mtext), 1, 0);
        printf("%s\n", rmsg.mtext);
        _exit(0);
    }
    struct Msg msg;
    msg.mtype = 1;
    strncpy(msg.mtext, "merhaba mq", sizeof(msg.mtext) - 1);
    msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
    wait(NULL);
    msgctl(msqid, IPC_RMID, NULL);
    return 0;
}

Expected output

merhaba mq

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does msgget(IPC_PRIVATE, flags) produce?

  • Existing queue
  • New unique queue Correct answer
  • Empty message
  • Deleted queue

Explanation: IPC_PRIVATE always produces a new and unique queue.

2 Match message queue functions

Term Meaning
msgget Creates queue
msgsnd Sends message
msgrcv Receives message
msgctl Removes queue

3 msgsnd call

Question
msgsnd(msqid, &msg, sizeof(msg.mtext), ____);
Answer
msgsnd(msqid, &msg, sizeof(msg.mtext), 0);

Explanation: The 0 flag waits until the queue has space.

All lessons in Advanced