INTERPROCESS COMMUNICATION USING SHARED MEMORY
#include “ stdio.h “
#include “ stdlib.h “
#include “ string.h “
#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ sys/shm.h “
#define SHMSIZE 1024
int main()
{
key_t key;
int shmid,ch=0;
struct shmid_ds s;
char d[10];
char *data;
int mode;
while(ch “ =7)
{
printf("\n1.Create");
printf("\n2.Attach");
printf("\n3.Operation");
printf("\n4.Retrieve");
printf("\n5.Detach");
printf("\n6.Check Struct value after attaching");
printf("\n7.Exit");
printf("\nEnter Ur Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((key=ftok("hema.c",'R'))==-1)
{
perror("ftok");
exit(1);
}
if((shmid=shmget(key,SHMSIZE,10644IPC_CREAT))==1)
{
perror("shmget");
exit(1);
}
break;
case 2:
data=shmat(shmid,NULL,0);
if(data==(char *)(-1))
{
perror("shmat");
exit(1);
}
break;
case 3:
printf("\nWriting to segment");
printf("\nEnter the data:");
scanf("%s",d);
strcpy(data,d);
break;
case 4:
printf("\nSegment contains %s",data);
break;
case 5:
if(shmdt(data)==-1)
{
perror("shmdt");
exit(1);
}
break;
case 6:
shmctl(shmid,IPC_STAT,&s);
printf("\nProcess Id :%d",getpid());
printf("\tTime=%time \t attach %d",s.shm_atime,s.shm_nattch);
system("ipcs_mhead");
break;
case 7:
exit(0);
}
}
return 0;
}
OUTPUT
[user1@localhost user1]$ cc shared.c
[user1@localhost user1]$ ./a.out
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:1
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:2
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:3
Writing to segment
Enter the data:welcome
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:4
Segment contains welcome
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:5
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:6
sh: line 1: ipcs_m: command not found
Process Id :4249 Time=1228356735me attach 0
1.Create
2.Attach
3.Operation
4.Retrieve
5.Detach
6.Check Struct value after attaching
7.Exit
Enter Ur Choice:7
[user1@localhost user1]$
Thursday, February 4, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment