C Program to Implement Inter Process Communication [IPC] System Programming
Inter-process communication (IPC) is the activity of sharing data across multiple and commonly specialized processes using communication protocols. Typically, applications using IPC are categorized as clients and servers, where the client requests data and the server responds to client requests. Many applications are both clients and servers, as commonly seen in distributed computing. Methods for achieving IPC are divided into categories which vary based on software requirements, such as performance and modularity requirements, and system circumstances, such as network bandwidth and latency.
There are several reasons for implementing inter-process communication systems:
Sharing information; for example, web servers use IPC to share web documents and media with users through a web browser.
Distributing labor across systems; for example, Wikipedia uses multiple servers that communicate with one another using IPC to process user requests.
Privilege separation; for example, HMI software systems are separated into layers based on privileges to minimize the risk of attacks. These layers communicate with one another using encrypted IPC.
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include "msg.h"
main()
{
int fd1[2],fd2[2],childpid;
pipe(fd1);
pipe(fd2);
childpid=fork();
if(childpid==0)
{
close(fd2[0]);
close(fd1[1]);
server(fd1[0],fd2[1]);
}
else
{
close(fd1[0]);
close(fd2[1]);
client(fd2[0],fd1[1]);
}
}
void client(int readfd,int writefd)
{
int n,length;
struct message mesg;
printf("Enter the Filenamen");
fgets(mesg.message_data,MAXMESSAGEDATA,stdin);
length=strlen(mesg.message_data);
if(mesg.message_data[length-1]=='n') length--;
mesg.message_length=length;
write(writefd,&mesg,MESGHDRSIZE+mesg.message_length);
while(1)
{
read(readfd,&mesg,MESGHDRSIZE);
length=mesg.message_length;
n=read(readfd,mesg.message_data,length);
write(STDOUT_FILENO,mesg.message_data,n);
}
}
void server(int readfd,int writefd)
{
int n,length;
struct message mesg;
FILE *fp;
n=read(readfd,&mesg,MESGHDRSIZE);
length=mesg.message_length;
n=read(readfd,mesg.message_data,length);
mesg.message_data[n]='