Basic example message queues reader and writer
In this program, you are going to learn
How to open a message queue ?
How to get/set message queue attributes ?
How to receive a message from a message queue?
How to send a message to a message queue?
How to use message queues APIs ?
Topics in this section,
Topics in this section,
Let us answer few basic questions in this message queues
What does mq_open do in this context?
See Answer
mq_open is used to open or create a message queue.
It creates a new message queue or opens an existing one identified by queue_name.
Why use O_CREAT | O_RDWR as flags?
See Answer
O_CREAT ensures the creation of the message queue if it does not exist, and
O_RDWR grants read and write access to the message queue.
What is the significance of the 0666 permission parameter?
See Answer
It sets the permission bits for the message queue, allowing read and write access for all users.
Can mq_open open an existing queue?
See Answer
Yes, if the queue with the specified name already exists, mq_open will open that existing queue.
What happens if the queue with queue_name does not exist?
See Answer
If the queue does not exist, and O_CREAT is specified, mq_open will create a new queue with the given name.
How does mq_open handle errors during queue creation?
See Answer
mq_open returns -1 on failure. You can check errno for specific error details.
Common issues include insufficient permissions or system resources.
Why pass NULL as the last parameter?
See Answer
The last parameter (struct mq_attr) is used for setting message queue attributes.
Passing NULL indicates using default attributes.
Can multiple processes open the same message queue?
See Answer
Yes, multiple processes can open the same message queue by using the same queue_name.
What is the lifetime of the message queue created with mq_open?
See Answer
The message queue persists until it is explicitly closed using mq_close or until the system is shut down.
How do I unlink (delete) a message queue created with mq_open?
See Answer
You can use mq_unlink(queue_name) to unlink (delete) the message queue.
This does not close the queue but removes its reference in the file system.
There are many functions used in message queues. We can classify those functions based on functionalities.
mq_open
mq_getattr
mq_receive
mq_send
mq_close
mq_openis used to open message queues. For example,
mq = mq_open(queue_name, O_CREAT | O_RDWR, 0666, NULL);
mq_getattris used to get message queue attributes. For example,
mq_getattr(mq, &attr);
mq_receiveis used to receive a message from a message queue. For example,
ret = mq_receive(mq, buffer, attr.mq_msgsize, NULL);;
mq_sendis used to send a message to a message queue. For example,
ret = mq_send(mq, message, strlen(message) + 1, 0);
mq_closeis used to close the opened message queues. For example,
mq_close(mq);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <unistd.h>
#include <signal.h>
int main(void)
{
struct mq_attr attr;
char* buffer;
int ret;
mqd_t mq;
const char* queue_name = "/my_queue";
const char* message = "Hello, Writer!";
mq = mq_open(queue_name,
O_CREAT | O_RDWR, 0666, NULL);
if (mq == (mqd_t)-1) {
perror("mq_open");
return -1;
}
ret = mq_getattr(mq, &attr);
if (ret < 0) {
perror("mq_getattr");
(void)close(mq);
return -2;
}
buffer = (char*)malloc(
attr.mq_msgsize);
if (buffer == NULL) {
perror("malloc");
(void)close(mq);
return -3;
}
ret = mq_receive(mq, buffer,
attr.mq_msgsize, NULL);
if (ret < 0) {
perror("mq_receive");
(void)close(mq);
return -4;
}
printf("Received message: %s\n",
buffer);
free(buffer);
ret = mq_send(mq, message,
strlen(message) + 1, 0);
if (ret < 0) {
perror("mq_send");
(void)close(mq);
return -5;
} else {
printf("messageSent = %s\n",
message);
}
(void)mq_close(mq);
return 0;
}
1$ gcc -o reader reader.c -lrt
2
3$ sudo ./reader
4
5Received message: Hello, reader!
6messageSent = Hello, Writer!
There are many functions used in message queues. We can classify those functions based on functionalities.
mq_open
mq_send
mq_getattr
mq_receive
mq_close
mq_openis used to open message queues. For example,
mq = mq_open(queue_name, O_CREAT | O_RDWR, 0666, NULL);
mq_sendis used to send a message to a message queue. For example,
ret = mq_send(mq, message, strlen(message) + 1, 0);
mq_getattris used to get message queue attributes. For example,
ret = mq_getattr(mq, &attr);
mq_receiveis used to receive a message from a message queue. For example,
ret = mq_receive(mq, buffer, attr.mq_msgsize, NULL);;
mq_closeis used to close the opened message queues. For example,
mq_close(mq);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <unistd.h>
#include <signal.h>
int main(void)
{
mqd_t mq;
int ret;
char *buffer;
const char* queue_name = "/my_queue";
const char* message = "Hello, reader!";
struct mq_attr attr;
mq = mq_open(queue_name,
O_CREAT | O_RDWR, 0666, NULL);
if (mq == (mqd_t)-1) {
perror("mq_open");
return -1;
}
ret = mq_send(mq, message,
strlen(message) + 1, 0);
if (ret < 0) {
perror("mq_send");
(void)close(mq);
return -2;
} else {
printf("messageSent = %s\n",
message);
}
ret = mq_getattr(mq, &attr);
if(ret < 0) {
perror("mq_getattr");
(void)close(mq);
return -3;
}
buffer = (char*)malloc(
attr.mq_msgsize);
if (buffer == NULL) {
perror("malloc");
(void)close(mq);
return -4;
}
memset(buffer, 0,
sizeof(buffer));
ret = mq_receive(mq, buffer,
attr.mq_msgsize, NULL);
if (ret < 0) {
perror("mq_receive");
(void)close(mq);
return -5;
}
printf("Received message: %s\n",
buffer);
free(buffer);
(void)mq_close(mq);
return 0;
}
1$ gcc -o writer writer.c -lrt
2
3$ sudo ./writer
4
5messageSent = Hello, reader!
6Received message: Hello, Writer!
There are many functions used in message queues. We can classify those functions based on functionalities.
mq_open
mq_getattr
mq_receive
mq_send
mq_close
mq_openis used to open message queues. For example,
mq = mq_open(queue_name, O_CREAT | O_RDWR, 0666, NULL);
mq_getattris used to get message queue attributes. For example,
mq_getattr(mq, &attr);
mq_receiveis used to receive a message from a message queue. For example,
ret = mq_receive(mq, buffer, attr.mq_msgsize, NULL);;
mq_sendis used to send a message to a message queue. For example,
ret = mq_send(mq, message, strlen(message) + 1, 0);
mq_closeis used to close the opened message queues. For example,
mq_close(mq);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <unistd.h>
#include <signal.h>
#define NUM_MESSAGES 10
int main(void)
{
struct mq_attr attr;
char* buffer;
int ret, i;
mqd_t mq;
const char* queue_name = "/my_queue";
const char* message = "Hello, Writer!";
mq = mq_open(queue_name,
O_CREAT | O_RDWR, 0666, NULL);
if (mq == (mqd_t)-1) {
perror("mq_open");
return -1;
}
ret = mq_getattr(mq, &attr);
if (ret < 0) {
perror("mq_getattr");
(void)close(mq);
return -2;
}
i = 0;
while (i < NUM_MESSAGES) {
buffer = (char*)malloc(
attr.mq_msgsize);
if (buffer == NULL) {
perror("malloc");
(void)close(mq);
return -3;
}
ret = mq_receive(mq, buffer,
attr.mq_msgsize, NULL);
if (ret < 0) {
perror("mq_receive");
(void)close(mq);
return -4;
}
printf("Received message: %s\n",
buffer);
free(buffer);
ret = mq_send(mq, message,
strlen(message) + 1, 0);
if (ret < 0) {
perror("mq_send");
(void)close(mq);
return -5;
} else {
printf("messageSent = %s\n",
message);
}
++i;
}
(void)mq_close(mq);
return 0;
}
1$ gcc -o reader reader.c -lrt
2
3$ sudo ./reader
4
5Received message: Hello, reader!
6messageSent = Hello, Writer!
7Received message: Hello, reader!
8messageSent = Hello, Writer!
9Received message: Hello, reader!
10messageSent = Hello, Writer!
11Received message: Hello, reader!
12messageSent = Hello, Writer!
13Received message: Hello, reader!
14messageSent = Hello, Writer!
15Received message: Hello, reader!
16messageSent = Hello, Writer!
17Received message: Hello, reader!
18messageSent = Hello, Writer!
19Received message: Hello, reader!
20messageSent = Hello, Writer!
21Received message: Hello, reader!
22messageSent = Hello, Writer!
23Received message: Hello, reader!
24messageSent = Hello, Writer!
There are many functions used in message queues. We can classify those functions based on functionalities.
mq_open
mq_send
mq_getattr
mq_receive
mq_close
mq_openis used to open message queues. For example,
mq = mq_open(queue_name, O_CREAT | O_RDWR, 0666, NULL);
mq_sendis used to send a message to a message queue. For example,
ret = mq_send(mq, message, strlen(message) + 1, 0);
mq_getattris used to get message queue attributes. For example,
ret = mq_getattr(mq, &attr);
mq_receiveis used to receive a message from a message queue. For example,
ret = mq_receive(mq, buffer, attr.mq_msgsize, NULL);;
mq_closeis used to close the opened message queues. For example,
mq_close(mq);
See the full program below,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mqueue.h>
#include <unistd.h>
#include <signal.h>
int main(void)
{
mqd_t mq;
int ret;
char *buffer;
const char* queue_name = "/my_queue";
const char* message = "Hello, reader!";
struct mq_attr attr;
mq = mq_open(queue_name,
O_CREAT | O_RDWR, 0666, NULL);
if (mq == (mqd_t)-1) {
perror("mq_open");
return -1;
}
ret = mq_send(mq, message,
strlen(message) + 1, 0);
if (ret < 0) {
perror("mq_send");
(void)close(mq);
return -2;
} else {
printf("messageSent = %s\n",
message);
}
ret = mq_getattr(mq, &attr);
if(ret < 0) {
perror("mq_getattr");
(void)close(mq);
return -3;
}
buffer = (char*)malloc(
attr.mq_msgsize);
if (buffer == NULL) {
perror("malloc");
(void)close(mq);
return -4;
}
memset(buffer, 0,
sizeof(buffer));
ret = mq_receive(mq, buffer,
attr.mq_msgsize, NULL);
if (ret < 0) {
perror("mq_receive");
(void)close(mq);
return -5;
}
printf("Received message: %s\n",
buffer);
free(buffer);
(void)mq_close(mq);
return 0;
}
1$ gcc -o writer writer.c -lrt
2
3$ sudo ./writer
4
5messageSent = Hello, reader!
6Received message: Hello, Writer!
7messageSent = Hello, reader!
8Received message: Hello, Writer!
9messageSent = Hello, reader!
10Received message: Hello, Writer!
11messageSent = Hello, reader!
12Received message: Hello, Writer!
13messageSent = Hello, reader!
14Received message: Hello, reader!
15messageSent = Hello, reader!
16Received message: Hello, reader!
17messageSent = Hello, reader!
18Received message: Hello, reader!
19messageSent = Hello, reader!
20Received message: Hello, reader!
21messageSent = Hello, reader!
22Received message: Hello, reader!
23messageSent = Hello, reader!
24Received message: Hello, reader!
Message queues API |
Learning |
|---|---|
mq_open |
creates a new message queue or opens an existing one, depending on the specified flags and parameters. |
mq_getattr |
is employed to obtain the attributes of an open message queue, including its current state and configuration. |
mq_receive |
receiving (reading) messages from a message queue. |
mq_send |
sending (writing) messages to a message queue. |
mq_close |
used to close a message queue descriptor. |
Previous topic
Current topic
Other IPCs