Inter Process Communication in UNIX
1)
System V is universally accepted and
most successful version of UNIX Operating System. This version of UNIX provides
multiple things to other Operating System such as
i)
TCP/IP Protocol suite.
ii)
Multiple IPC Techniques.
iii)
Concept of Pipe.
iv)
Concept of Socket Programming
etc.
2)
We can perform Inter Process
Communication by using different techniques such as
i)
Shared Memory.
ii)
Message Queue.
iii)
Pipe.
iv)
Memory Map Files.
v)
FIFO (Queue).
3)
Each an every IPC techniques
has different perspective depend on the data should be send and the data which
can be received by the other processes.
1)
Shared Memory :-
i)
Shared memory is the fastest
way of process communication.
ii)
In case of shared memory the
actual memory gets allocated in address space of the sender process.
iii)
That address space is share by
the receiver process.
iv)
That two process can
communicate with each other by writing and reading the data from that shared
memory.
v)
To demonstrate the concept of
shared memory there should be at least two process on same machine.
vi)
There are some steps which are
followed by the sender process as
i)
Decide the key for
communication.
ii)
Call the function shmget()
to allocate shared memory.
iii)
Call the function shmat() which is use to attach that
shared memory to our address .
iv)
Write the data inside that
allocated memory.
v)
Detach that allocated memory by
calling shmdt()function.
v) Steps followed by the receiver process as
i.
Use the key which is use by the
sender process for communication.
ii.
Call the function shmget() to locate that shared memory
segment which is allocated by sender process.
iii.
Call the function shmat() to attach that segment.
iv.
Read the data from that shared
memory segment.
v. Deallocate that memory by calling shmdt() function.----------------------------------------------------------------------------------------------------------------------------------
/* Sender process program */
/* Program Name: Sender.c
----------------------------------------------------------------------------------------------------------------------------------
/* This program demostrate the concept of Shared Memory in Unix. */
#include<stdio.h>
#include<sys/shm.h>
#define SHMSIZE 256
int main()
{
char *shm, *s, ch;
int shmid;
key_t key;
key = 9999;
/* shmget() returns the identifier of the shared memory segment associated with the value of the argument key.
This function is use to allocate the shared memory.
Parameters:
1. Required key value which known by sender and receiver.
2. Size of shared memory.
3. shared memory flag and permission.
*/
shmid = shmget(key, SHMSIZE, IPC_CREAT | 0666);
/* shmat() attaches the shared memory segment identified by shmid to the address space of the calling process.
Parameters:
1. Return value of shmget() function.
2. If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.
3. shared memory flags.
*/
shm = shmat(shmid, NULL, 0);
s = shm;
for(ch = 'a'; ch <= 'z'; ch++)
{
*s = ch;
s++;
}
*s = NULL;
while(*shm != '*')
sleep(1);
shmdt(shm);
return 0;
}
------------------------------------------------------------------------------------------------------------
/* Receiver process program */
/* Program Name: Receiver.c
------------------------------------------------------------------------------------------------------------
/* This program demostrate the concept of Shared Memory in Unix. */
#include<stdio.h>
#include<sys/shm.h>
#define SHMSIZE 256
int main()
{
char *shm, *s, ch;
int shmid;
key_t key;
key = 9999;
/* shmget() returns the identifier of the shared memory segment associated with the value of the argument key.
This function is use to allocate the shared memory.
Parameters:
1. Required key value which known by sender and receiver.
2. Size of shared memory.
3. shared memory flag and permission.
*/
shmid = shmget(key, SHMSIZE, 0666);
/* shmat() attaches the shared memory segment identified by shmid to the address space of the calling process.
Parameters:
1. Return value of shmget() function.
2. If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.
3. shared memory flags.
*/
shm = shmat(shmid, NULL, 0);
for(s = shm; *s != NULL; s++)
{
printf("%c",*s);
}
printf("\n");
*shm = '*';
shmdt(shm);
return 0;
}
------------------------------------------------------------------------------------------------------------
Steps to compile it.
1) gcc -o sender Sender.c
2) gcc -o receiver Receiver.c
3) Open two terminal at time to see the output.
4) ./sender
5) ./receiver.
------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment