Wednesday, April 20, 2016

IPC: Message queue with example

Message queue

Message queue is a type of inter process communication. It is used to transfer data between processes. It is asynchronous communication as in, sender can dump data in queue, and receiver can get the data out at its convenience.

Parameters of queue:

  1. Queue ID
  2. Key
  3. Message structure:
    • type
    • text

Method calls:

  1. msgget()
  2. msgsnd()
  3. msgrcv()

Applications:

  1. VxWorks and QNX encourage the use of message queue for inter-process & inter-thread communication
  2. It provides resilience functionality as the message dont get "lost" in communication in case of system failure.

Example:

Sender code - creates queue and enter message into it with a particular key
Receiver code - Access the queue, gets the message and prints the text. If while receiver process execution data is not available in queue, it waits at msgrcv(), and proceeds when it gets some data.

Shell command to view the queue IPC
$ipcs -q

*******************************
sender.c
*******************************
/*
 * sender.c
 *
 *  Created on: 19-Apr-2016
 *      Author: root
 */

//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);
}

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

main()
{
int count = 0;
//char data[3][MAXSIZE] = {"first", "second", "third"};
    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 ;

    while (count < 3){
    //*sbuf.mtext = data[count];
    //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");
}
count++;
    }
    exit(0);
}

*******************************
receiver.c
*******************************
/*
 * receiver.c
 *
 *  Created on: 19-Apr-2016
 *      Author: root
 */

//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);
}

--------------------------------------------------------------------------------------------------

OUTPUT:

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    


$./sender
Enter a message to add to message queue : text data to trasmit
Message Sent
Message Sent
Message Sent

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x000004d2 0          root       666        63           3           

$./receiver
text data to trasmit

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x000004d2 0          root       666        42           2           

$./receiver
text data to trasmit

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x000004d2 0          root       666        21           1           

$./receiver
text data to trasmit

$ ipcs -q

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x000004d2 0          root       666        0            0           


No comments:

Post a Comment

PROFILE

My photo
India
Design Engineer ( IFM Engineering Private Limited )

Followers