Saturday, March 31, 2012

String manipulation using message Queue

This program is used to perform the string manipulation using message Queue.contains 4 programs.one program is used as the header.one
program for used to send the data int the messga queue and other two programs are used to reterive the message.

Queue.h
-------
#include<stdio.h>
#include<errno.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct queue
{
        long type;
        char msg[50];
};

Queue.c
-------

#include "queue.h"
int main()
{
       struct queue q;
       int mid;
       if((mid=msgget(ftok("string",0x99),0644|IPC_CREAT))<0)
       {
               if(mid!=-1)
               {
                       printf("Message Queue Error
");
                       exit(1);
               }
               else
               {
                       printf("Message Already Exist
");
                       exit(1);
               }
       }
       printf("
Enter the Message Type:");
       scanf("%d",&q.type);
       while(q.type>0)
       {
               printf("
Enter the Message:");
               getchar();
               gets(q.msg);
               if(msgsnd(mid,&q,sizeof(q),IPC_NOWAIT)<0)
               {
                       printf("Message send Error
");
                       exit(1);
               }
               printf("
Enter the Message Type:");
               scanf("%d",&q.type);
       }
return 0;
}


Word.c
------

#include "queue.h"
#include<string.h>
void word(char* str)
{
       int i,n,w=1;
       n=strlen(str);
       for(i=0;i<n;i++)
               if(str[i]==' ' && str[i-1]!=' ')
                       w++;
       printf("Number of Words in "%s" :%d
",str,w);
}

int main()
{
       struct queue q;
       int mid;
       long type;
       if((mid=msgget(ftok("string",0x99),0644 ))<0)
       {
               printf("Message Queue Error
");
               exit(1);
       }
       printf("
Enter the Message Type to Reterive:");
       scanf("%d",&type);
       while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
       {
               word(q.msg);
       }
       msgctl(mid,IPC_RMID,NULL);
}

Reverse.c
---------

#include "queue.h"
#include<string.h>
void rev(char* str)
{
       char s[30];
       int i,j,n;
       n=strlen(str);
       printf("Reverse for "%s" :",str);
       for(i=n;i>=0;i--)
               putchar(str[i]);
       printf("
");
}
int main()
{
       struct queue q;
       int mid;
       long type;
       if((mid=msgget(ftok("string",0x99),0644 ))<0)
       {
               printf("Message Queue Error
");
               exit(1);
       }
       printf("
Enter the Message Type to Reterive:");
       scanf("%d",&type);
       while(msgrcv(mid,&q,sizeof(q),type,IPC_NOWAIT) && errno!=ENOMSG)
       {
               rev(q.msg);
       }
}

 

No comments:

Post a Comment