In a multitasking Linux environment, multiple processes often need to exchange data and coordinate their actions. While shared memory offers fast access, it can become complex when synchronization is required, especially with frequent read/write operations or when only selective processes need to communicate. This is where System V Message Queues come into play—a structured and reliable form of Interprocess Communication (IPC).
Message queues allow processes to send and receive messages in a FIFO (First-In, First-Out) manner, ensuring orderly and isolated data transfer without the need for shared memory space. They support multiple message types, enabling targeted communication between specific processes. Ideal for applications dealing with small, discrete data packets, message queues simplify process interaction with built-in message categorization and queue control.
In this tutorial, you’ll learn the core system calls—msgget()
, msgsnd()
, msgrcv()
, and msgctl()
—used to implement message queue communication in C. We’ll also walk through a hands-on example where one process sends and another receives messages, giving you practical experience in building efficient Linux IPC systems.
Why do we need message queues when we already have the shared memory? It would be for multiple reasons, let us try to break this into multiple points for simplification –
Using shared memory or message queues depends on the need of the application and how effectively it can be utilized.
Communication using message queues can happen in the following ways –
Writing into the shared memory by one process and reading from the shared memory by another process. As we are aware, reading can be done with multiple processes as well
Writing into the shared memory by one process with different data packets and reading from it by multiple processes, i.e as per message type.
Having seen certain information on message queues ,Now it is time to check for the system call (system V) Which supports the message queues.
To perform communication using message queues, following are the steps
Step 1:- Create a message queue or connect to an already existing message queue(msgget())
Step 2:- Write into message queue(msgsnd())
Step 3:- Read from the message queue(msgrcv())
Step 4:- Perform control operations on the message queue(msgctl())
Now ,let us check the syntax and certain information on the above calls.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg)
This system call creates or allocates a system V message queue. Following arguments need to be passed –
The first argument ,Key ,recognizes the message queue. The key can be either an arbitrary value or one that can be derives from the library function ftok().
The second argument ,Shmflg, Specifies the required message queue flag/s such as IPC_CREAT (Creating message queue if not exists) or IPC_EXCL(Used with IPC_CREAT to create the message queue and the call fails, if the message queue already exists). Need to pass the permissions as well.
Note:- Refer earlier sections for details on permissions.
This call would return a valid message queue identifier(Used for further calls of message queue) on success and -1 in case of failure To know the cause of failure, check with errno variable or perror() function.
Various errors with respect to this call are EACCESS (Permission denied),
EEXIST (Queue already exists cant create),ENOENT(queue doesn’t exist),ENOMEM(not enough memory to creat the queue),etc.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
This system call send/Appends a message into the message queue(System V) Following arguments need to be passed –
struct msgbuf
{
long mtype;
char mtext[1];
};
The variable mtype is used for communicating with different message types. The variable mtext is an array or other structure whose size is specified by msgsz(positive value). If the mtext field is not mentioned, then it is considered as zero size message ,Which is permitted.
This call would return the number of bytes actually received in mtext array on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgctl(int msgid, int cmd, struct msqid_ds *buf)
This system call performs control operations of the message queue (System V). Following arguments need to be passed –
Steps for Message Queue Communication in C
Step 1 – Create two separate processes:
Step 2 – Create a unique key using the ftok() function.
Step 3 – In the sending process, perform the following:
Indian Institute of Embedded Systems – IIES