Thursday, February 4, 2010

MESSAGE QUEUE

MESSAGE QUEUE


#include “ stdio.h “
#include “ stdlib.h “
#include “ sys/types.h “
#include “ sys/ipc.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;
while(cont)
{
printf("\n\t Message Queue Manipulation ");
printf("\n1.Create message queue");
printf("\n2.Add a message into the queue");
printf("\n3.Retrive a message from the queue");
printf("\n4.Remove a message queue");
printf("\n5.Exit");
printf("\nEnter 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\t------- -------");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
printf("\nEnter the message ");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeofmsg,0);


if(ren!=1)
printf("\nMessage posted successfully");
else
printf("\nSorry you made a mistake");
break;
case 3:
printf("\nMessage receving & removing from the queue");
printf("\nEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeofmsg,msg.mtype,0);
printf("\nMessage type %d",msg.mtype);
printf("\nMessage length %d",msglen);
printf("\nMessage 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\t Enter the value choice");
break;
}
printf("\nDo you want to contine y=1/n=0 : ");
scanf("%d",&cont);
}
}

No comments:

Post a Comment