MESSAGE QUEUES
UNIX SYSTEM PROGRAMMING
INTRODUCTION
Message queues allow messages to be passed from one process to another. There can be multiple
writers to the queue as well as multiple readers.
Message queues are often used for passing small messages between processes. They can also be
used for process synchronization.
System V Message and POSIX message queue
Message Queue provides an asynchronous communications protocol, a system that puts a message
onto a message queue does not require an immediate response to continue processing”
WHY DO WE NEED ?
➢ As understood, once the message is received by a process it would be no longer available for any
other process. Whereas in shared memory, the data is available for multiple processes to access.
➢ If we want to communicate with small message formats.
➢ Shared memory data need to be protected with synchronization when multiple processes
communicating at the same time.
➢ The order of message queue is FIFO (First In First Out).
ARCHITECTURE
HOW WILL IT PROCESS ?
➢ Writing into the shared memory by
one process and reading from the
shared memory by another process.
➢ Writing into the shared memory by
one process with different data
packets and reading from it by
multiple processes.
SYSTEM CALL
Step 1 : msgget()
Step 2 : msgsnd()
Step 3 : msgrcv()
Step 4 : msgctl()
STEP 1
The first function normally called is msgget to either open an existing queue or create a new queue.
#include <sys/msg.h>
int msgget(key_t key, int flag);
Returns: message queue ID if OK, 1 on error
On success, msgget returns the non-negative queue ID. This value is then used with the other three
message queue functions.
STEP 2
Data is placed onto a message queue by calling msgsnd.
#include <sys/msg.h>
int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag);
Returns: 0 if OK, 1 on error.
The ptr argument is then a pointer to a mymesg structure. The message type can be used by the
receiver to fetch messages in an order other than first in, first out.
STEP 3
Messages are retrieved from a queue by msgrcv.
#include<sys/msg.h>
ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag);
Returns: size of data portion of message if OK, 1 on error.
STEP 4
The msgctl function performs various operations on a queue.
#include<sys/msg.h>
int msgctl(int msqid, int cmd, struct msqid_ds *buf );
Returns: 0 if OK, 1 on error.
BENEFITS
● Reliable message delivery: With this feature in place, additional de-duplication or
loss-prevention logic is unnecessary.
● Inter-application connectivity: This simplifies application development and enables
disparate architectures to work together.
BENEFITS
● Versatility: Message queue solutions can support multiple languages.They can also
support numerous application programing interfaces (APIs) and protocols.
● Resilience: Asynchronous messaging ensures application-specific faults won’t impact
the system.
BENEFITS
● Improved security: This can contribute to the overall security of the applications
and/or infrastructure.
● Integrated file transfers: This can be used as an alternative to FTP within enterprises
where such solutions are in use.
WEB SERVICE vs MESSAGE QUEUE
LIMITS
1. Total size of all messages in a queue.
2. Number of messages in a queue.
3. Size of a message
4. Number of queues.
USE CASE
Web service always has to be highly available and ready to receive new requests instead of being
locked by the processing of previous received requests.
In this case it is ideal to put a queue between the web service and the processing service.
UNIX APIs for Messages
POSIX message queue system calls:
mq_open
mq_close
mq_unlink
mq_send
mq_receive
mq_timedsend
mq_timedreceive
mq_notify
mq_getattr
mq_setattr
System V message queue system calls:
msgget
msgctl
msgsnd
msgrcv
CONCLUSION
Message queues are often used for passing small messages between processes. They can also be used
for process synchronization.
One process establishes a message queue that others may access. Often a server will establish a
message queue that multiple clients can access
Processes must share a common key in order to gain access to the queue in the first place.
THANK YOU

Usp message queues

  • 1.
  • 2.
    INTRODUCTION Message queues allowmessages to be passed from one process to another. There can be multiple writers to the queue as well as multiple readers. Message queues are often used for passing small messages between processes. They can also be used for process synchronization. System V Message and POSIX message queue Message Queue provides an asynchronous communications protocol, a system that puts a message onto a message queue does not require an immediate response to continue processing”
  • 3.
    WHY DO WENEED ? ➢ As understood, once the message is received by a process it would be no longer available for any other process. Whereas in shared memory, the data is available for multiple processes to access. ➢ If we want to communicate with small message formats. ➢ Shared memory data need to be protected with synchronization when multiple processes communicating at the same time. ➢ The order of message queue is FIFO (First In First Out).
  • 4.
  • 5.
    HOW WILL ITPROCESS ? ➢ Writing into the shared memory by one process and reading from the shared memory by another process. ➢ Writing into the shared memory by one process with different data packets and reading from it by multiple processes.
  • 6.
    SYSTEM CALL Step 1: msgget() Step 2 : msgsnd() Step 3 : msgrcv() Step 4 : msgctl()
  • 7.
    STEP 1 The firstfunction normally called is msgget to either open an existing queue or create a new queue. #include <sys/msg.h> int msgget(key_t key, int flag); Returns: message queue ID if OK, 1 on error On success, msgget returns the non-negative queue ID. This value is then used with the other three message queue functions.
  • 8.
    STEP 2 Data isplaced onto a message queue by calling msgsnd. #include <sys/msg.h> int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag); Returns: 0 if OK, 1 on error. The ptr argument is then a pointer to a mymesg structure. The message type can be used by the receiver to fetch messages in an order other than first in, first out.
  • 9.
    STEP 3 Messages areretrieved from a queue by msgrcv. #include<sys/msg.h> ssize_t msgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag); Returns: size of data portion of message if OK, 1 on error.
  • 10.
    STEP 4 The msgctlfunction performs various operations on a queue. #include<sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf ); Returns: 0 if OK, 1 on error.
  • 11.
    BENEFITS ● Reliable messagedelivery: With this feature in place, additional de-duplication or loss-prevention logic is unnecessary. ● Inter-application connectivity: This simplifies application development and enables disparate architectures to work together.
  • 12.
    BENEFITS ● Versatility: Messagequeue solutions can support multiple languages.They can also support numerous application programing interfaces (APIs) and protocols. ● Resilience: Asynchronous messaging ensures application-specific faults won’t impact the system.
  • 13.
    BENEFITS ● Improved security:This can contribute to the overall security of the applications and/or infrastructure. ● Integrated file transfers: This can be used as an alternative to FTP within enterprises where such solutions are in use.
  • 14.
    WEB SERVICE vsMESSAGE QUEUE
  • 15.
    LIMITS 1. Total sizeof all messages in a queue. 2. Number of messages in a queue. 3. Size of a message 4. Number of queues.
  • 16.
    USE CASE Web servicealways has to be highly available and ready to receive new requests instead of being locked by the processing of previous received requests. In this case it is ideal to put a queue between the web service and the processing service.
  • 17.
    UNIX APIs forMessages POSIX message queue system calls: mq_open mq_close mq_unlink mq_send mq_receive mq_timedsend mq_timedreceive mq_notify mq_getattr mq_setattr System V message queue system calls: msgget msgctl msgsnd msgrcv
  • 18.
    CONCLUSION Message queues areoften used for passing small messages between processes. They can also be used for process synchronization. One process establishes a message queue that others may access. Often a server will establish a message queue that multiple clients can access Processes must share a common key in order to gain access to the queue in the first place.
  • 19.