Google Ads

Sunday, January 3, 2010

MESSAGE QUEUES

INTERPROCESS COMMUNICATION USING
MESSAGE QUEUES


#include “ stdlib.h “
#include “ sys/types.h ”
#include “ sys/ipc.h ”
#include “ stdio.h “
#include " unistd.h “
#define msgtxtlen 100
#define sizeofmsg sizeof(msg)
#define msglen strlen(msg.mtext)
struct msgbuf
{
long mtype;
char mtext[msgtxtlen];
}msg;

main()
{
int ch;
int msgqid,cont=1,ren;
printf("\n\tMessage Queue Manipulation");
while(cont)
{
printf("\n\t1.Create message queue");
printf("\n\t2.Add a message into the queue");
printf("\n\t3.Retrieve a message from the queue");
printf("\n\t4.Remove a message queue");
printf("\n\t5.Exit");
printf("\n\tEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
msgqid=msgget(0x25,IPC_CREAT0644);
printf("\n\tMessage queue is created");
break;



case 2:
printf("\n\tMessage sending:");
printf("\n\tEnter the message type:");
scanf("%d",&msg.mtype);
printf("\n\tEnter the message:");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeof(msg),0);
if(ren!=1)
printf("\n\tMessage posted successfully");
else
printf("\n\tSorry u made mistake");
break;

case 3:
printf("\n\tMessage receiving and removing from the queue");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeof(msg),msg.mtype,0);
printf("\n\tMessage type :%d",msg.mtype);
printf("\n\tMessage length :%d",msglen);
printf("\n\tMessage content :%s",msg.mtext);
break;

case 4:
ren=msgctl(msgqid,IPC_RMID,NULL);
if(ren!=1)
printf("\n\tMessage queue was removed");
else
printf("\n\tSorry");
break;

case 5:
exit(0);
default:
printf("\n\tEnter the value choice\n");
break;
}
printf("\n\tDo u wish to continue. yes=1,no=0 :");
scanf("%d",&cont);
}
}

OUTPUT

[user1@localhost user1]$ cc msgq.c
[user1@localhost user1]$ ./a.out

Message Queue Manipulation

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:1

Message queue is created
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:2

Message sending:
Enter the message type:0

Enter the message:hello
Message posted successfully
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit


Enter the choice:3

Message receiving and removing from the queue
Enter the message type0

Message type :0
Message length :5
Message content :hello
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:4

Message queue was removed
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:5

[user1@localhost user1]$






No comments:

Post a Comment