Featured Post

Trie implementation in C

IPC Message Queue Implementation in C

A simple implementation of IPC Message Queues.
IPC_msgq_send.c adds the message on the message queue .
IPC_msgq_rcv.c removes the message from the message queue.

To use this program first compile and run IPC_msgq_send.c to add a message to the message queue. To see the Message Queue type ipcs -q on your Unix/Linux Terminal.

Now compile and run IPC_msgq_rcv.c to read the message from the Message Queue.
To see that you have read the message again use ipcs -q
//IPC_msgq_send.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE     128

void die(char *s)
{
  perror(s);
  exit(1);
}

typedef struct msgbuf
{
    long    mtype;
    char    mtext[MAXSIZE];
};

main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    struct msgbuf sbuf;
    size_t buflen;

    key = 1234;

    if ((msqid = msgget(key, msgflg )) < 0)   //Get the message queue ID for the given key
      die("msgget");

    //Message Type
    sbuf.mtype = 1;

    printf("Enter a message to add to message queue : ");
    scanf("%[^\n]",sbuf.mtext);
    getchar();

    buflen = strlen(sbuf.mtext) + 1 ;

    if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
    {
        printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
        die("msgsnd");
    }

    else
        printf("Message Sent\n");

    exit(0);
}


//IPC_msgq_rcv.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE     128

void die(char *s)
{
  perror(s);
  exit(1);
}

typedef struct msgbuf
{
    long    mtype;
    char    mtext[MAXSIZE];
} ;


main()
{
    int msqid;
    key_t key;
    struct msgbuf rcvbuffer;

    key = 1234;

    if ((msqid = msgget(key, 0666)) < 0)
      die("msgget()");


     //Receive an answer of message type 1.
    if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
      die("msgrcv");

    printf("%s\n", rcvbuffer.mtext);
    exit(0);
}

Comments

  1. very nice and simple code, helped me a lot , thanks....:)

    ReplyDelete
  2. thank you bhaiya...............it is really a simplest coding............thanks again

    ReplyDelete
    Replies
    1. yes the bhaiya helped us all...all hail bhaiya

      Delete
    2. i helped make you

      Delete
  3. yuppyyyyy..its working buddy!!thanx

    ReplyDelete
  4. error...in IPC_msgq_send.c
    msg_que_sent.c: In function ‘main’:
    msg_que_sent.c:45:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=]
    printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
    ^

    ReplyDelete
    Replies
    1. Hey, its not an error, its seems to be a warning. Probably our compiler versions were different. Not an issue until it's not compiling. You can use %ld if needed.

      Delete
    2. can u write another prgrm that do both things at same time means recive and send the message at same time and other program is also send and recive the message at same time means both programme send and recive...
      \it is possible???
      if yes than plz write the code..
      i think think this is possibble using two message queues.. but i didnt do it if u than plz help me...

      Delete
    3. use %ld for mytype and change s_size with int.theb it will successfully compile

      Delete
    4. i got an error it says variable has
      incomplete type 'struct msgbuf'
      struct msgbuf rcvbuffer;

      Delete
    5. i got 2 errors when running the code:

      1) variable has incomplete type
      'struct msgbuf'
      struct msgbuf rcvbuffer;

      Delete
    6. Please add typedef in front of struct declaration.

      Delete
    7. I have fixed that in the code.

      Delete
  5. how to take input from the file using pipes and then display the messge using grep command??

    ReplyDelete
  6. Thank you. Helped me to Understand the basics

    ReplyDelete
  7. Thanks Dude for Short and simple explanation.

    Can you throw some light on POSIX queue as well .if it is possible.

    ReplyDelete
  8. When i compile it give me error like this
    "msgget: Function not implemented".

    Please Give me a solution of it???
    How can i solve it???

    ReplyDelete
  9. have you included #include and #include headers

    ReplyDelete
  10. i didnt get the out

    ReplyDelete
  11. thanks for sharing simple understandable program.

    ReplyDelete
  12. when to use msg queue and when to use shared memory in IPC ? can u pls throw some light, thanks in advance

    ReplyDelete
    Replies



    1. In short Message queues are more reliable but slower as compared to shared memory. However, shared memory has synchronization issues. They both can be combined to achieve better results.

      Essentially if multiple processes need to update a chunk of data concurrently, you would go for shared memory.
      If you need a message passing kind of scenario where a mailbox kind of scenario exist where a process sends messages to a message queue and different processes read it from the queue one by one.

      Delete
  13. plz program for reader writer by using message queue.... any one can interested in these program..
    so plz reply me.. and provide the code

    ReplyDelete
  14. In sending part, on which basis we decided the buflen = strlen + 1 , as text length is strlen but mtype is long so why we have just added 1 byte instead of sizeof long

    ReplyDelete
  15. How do I run message queue program on windows????

    ReplyDelete
  16. This is not an implementation of message queue, rather this is an example app that uses the POSIX Message Queue.

    ReplyDelete
  17. Hello sir, whether it will run on putty server our lab programs is done in putty server

    ReplyDelete

Post a Comment

Please post your valuable suggestions