Saturday, January 2, 2016

Message Queue:-

1) This is the another way of Inter Process Communication. In which one process can communicate with other process by sending the message.

2) In this concept we have to send message to the operating system and that message is accepted by the operating system then that message gets inserted into a message queue by the operating system.

3) If receiver process is running then that process request to get the message to the operating system then depend on the id of that process operating system receive that message from the message queue and that message gets displayed to the receiver process.

4) The internal mechanism of message queue and shared memory is almost similar.
------------------------------------------------------------------------------------------------------------

Steps followed by the message sender.

1) Include header file as msg.h.

2) Decide the structure which is use to receive the message.

3) Decide the key which is use for communication.

4) Call the function msgget() to start the communication by specifying the flag as IPC_CREAT.

5) Call the function msgsnd() by filling the data inside that message structure.
------------------------------------------------------------------------------------------------------------

Steps followed by message receiver.

1) Include header file msg.h.

2) Use the communication key which is decided by the sender.

3) Call function msgget() by specifying that key.

4) Call function msgrcv() and pass the address of empty structure to receive that message.

------------------------------------------------------------------------------------------------------------
/* This program illustrate that the concept of Message Queue. */
/* Program Name:- Sender.c
------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#define MSGSIZE 256

struct message
{
int id;
char data[MSGSIZE];
};

int main()
{
int msgid;
key_t key;
struct message msg;

key = 9999;

/*
The  msgget()  system call returns the message queue identifier associated with the value of the key argument.
  */

msgid = msgget(key, IPC_CREAT | 0666);

msg.id = 1;

strcpy(msg.data, "Hello World");

/*  The  msgsnd() system calls are used to send messages to a message queue. The calling process     must  have write  permission on the message queue in order to send a message
*/
/* The msgsnd() system call appends a copy of the message  pointed  to  by msg to the message queue
  whose identifier is specified by msgid.
*/
msgsnd(msgid, &msg, MSGSIZE, 0);

return 0;
}

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

/* This program illustrate that the concept of Message Queue. */
/* Program Name:- Receiver.c */

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

#include<stdio.h>
#include<sys/msg.h>
#define MSGSIZE 256

struct message
{
int id;
char data[MSGSIZE];
};

int main()
{
int msgid;
key_t key;
struct message msg;
key = 9999;
/*
The  msgget()  system call returns the message queue identifier associated with the value of the key argument. 
  */
msgid = msgget(key, 0666);
/* The msgrcv() system calls are used to receive messages from a message queue. The calling process must have read permission to receive a message.
*/
/*
The msgrcv() system call removes a message from the queue specified by msgid and places it in the buffer pointed to by msgp.
*/
msgrcv(msgid, &msg, MSGSIZE,1, 0);
printf("%s",msg.data);
return 0;
}

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

For compilation read previous post.............

No comments:

Post a Comment

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

    SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cou...