Thursday 29 September 2011


IMPLEMENTATION OF INTERPROCESS COMMUNICATION USING SHARED MEMORY

AIM:
To implement interprocess communication using shared memory.

ALGORITHM:

1.Start the process.
2.Create the child process using fork().
3.Create the shared memory for parent process using shmget() system call.
4.Now allow the parent process to write in shared memory using shmptr pointer which is return type of shmat().
5.Now across and attach the same shared memory to the child process.
6.The data in the shared memory is read by the child process using the shmptr pointer.
7.Now, detach and reuse the shared memory.
8.Stop the process

PROGRAM:

#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child, shmid, I;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(4041,32,0666| IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf(“\nParent writing:\t”);
for(I=0;I<10;I++)
{
shmptr[I]=’a’+I;
putchar(shmptr[I]);
}
printf(“\n”);
wait(NULL);
}
else
{
shmid=shmget(4041,32,0666);
shmptr=shmat(shmid,0,0);
printf(“\nChild is reading:\t”);
for(I=0;I<10;I++)
putchar(shmptr[I]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
printf("\n");
return 0;
}

OUTPUT:

[root@localhost ~]# ./ipc

Parent writing:   abcdefghij

Child is reading: abcdefghij

RESULT: 
Thus a interprocess communication using shared memory was implemented in linux platform and the output was verified.

0 comments:

Post a Comment