Saturday, March 31, 2012

String manipulation using shared memory

string manipulation using shared memory....
"A Process say p1 has to create the shared memory to store a line of text.Another Process say p2 has to perform the substring operation and it
is displayed in process p2 itself."

 Process p1
----------

#include<sys/shm.h>
int main()
{
       size_t length;
       struct shmid_ds buff;
       char *ptr,p[50];
       int id,i;
       printf("
            Shared Memory");
       printf("
            ------ ------");
       if((id=shmget((key_t)6234,2,0664|IPC_CREAT))<0)
       {
               printf("Shared Memory Creation Error
");
               exit(1);
       }
       ptr=shmat(id,NULL,0);
       printf("
Enter the string :");
       gets(p);
       puts(p);
       for(i=0;i<strlen(p);i++)
               *ptr++=p[i];
       printf("The Entered String %s was Accepted.
",ptr);
       exit(0);
}


Process p2
----------

#include<stdio.h>
#include<sys/shm.h>
int main()
{
       size_t length;
       struct shmid_ds buff;
       char *ptr,a[50],b[10],c[20];
       int id,i=0,flag=0,j,k=0;
       if((id=shmget((key_t)6234,2,0664|IPC_CREAT))<0)
       {
               printf("Shared Memory Creation Error
");
               exit(1);
       }
       printf("
Shared memory Created : %d",id);
       ptr=shmat(id,NULL,0);
       memset(a,'

No comments:

Post a Comment