Saturday, July 23, 2011

Message Queues- An Introduction

Processes can exchange messages using Message Queues. Sending process message is saved in a queue, Receiving process reads the message from queue.

Below program sends message to queue.


Below snapshot is the code for receiving process which reads message from queue.


Compiling msg_snd.c:
#gcc msg_snd.c -o msg_snd

Compiling msg_recv.c:
#gcc msg_recv.c -o msg_recv

Output:
Snapshot below

To check the state of Message Queues run ipcs -q command.

Pointers:
1. Include headers
         sys/types.h
         sys/ipc.h
         sys/msg.h
2.  msgq_id (message id) is an arbitrary number of type int generated by msgget() which should be passed as parameter/argument to msgsnd(), msgrcv()
msgq_id can also be generated using ftok()
3. key (key_t is of type int) is another arbitrary number. Same key value must be passed as parameter/argument to msgsnd() and msgrcv().
4. Message Type (mtype) is another arbitrary number. Same mtype should be passed as parameter/argument to msgsnd(), msgrcv()
5. mtype is passed as struct mbuf argument to msgsnd(), and long type to msgrcv()
6. for man page of any API run man command e.g.
        man 2 msgsnd
        man ipcs

This is pretty high-level overview of Message Queues.
Feel free to drop a comment.

Other articles on C language
http://darshanams.blogspot.in/2011/09/building-single-binary-from-multiple-c.html
http://darshanams.blogspot.in/2012/01/endianness-different-processors.html

No comments:

Post a Comment