Write an IPC program using pipe. Process A accepts a character string and Process B inverses the string. Pipe is used to establish communication between A and B processes using Python or C++.

#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<unistd.h>
#define BUFSIZ 60
using namespace std;
int main()
{
int data_processed;
int file_pipe[2];
int len;
char str[20];
//const char some_data[] = "Data Written in pipe";
char buffer[BUFSIZ+1];
pid_t fork_result;
if(pipe(file_pipe) == 0)
{
fork_result = fork();
if(fork_result == -1){
cout<<"\nstderr"<<"Fork Failure";
exit(EXIT_FAILURE);
}
if(fork_result == 0){
data_processed =read(file_pipe[0], buffer, BUFSIZ);
cout<<"\nRead"<<data_processed<<" bytes:"<< buffer;
execlp("./kal","len",buffer,0);
exit(EXIT_SUCCESS);
}
else{ //Data written by parent process


cout<<"\nEnter the string:";
cin>>str;



//Data read by child process
data_processed = write(file_pipe[1], str, strlen(str));
cout<<"\nWrote"<<data_processed<<" bytes";
//exit(EXIT_SUCCESS);
int i,len=0,l=0;
char temp;

len=strlen(str);
l=len-1;
for(i=0;i<len/2;i++)
{
temp=str[i];
str[i]=str[l];
str[l]=temp;
l--;
}
cout<<"\nReverse string::"<<str;

}
exit(EXIT_SUCCESS);
return 0;
}
}

/******************OUTPUT**********************
[soetcomp@localhost ~]$ gedit 5.cpp
[soetcomp@localhost ~]$ g++ 5.cpp
[soetcomp@localhost ~]$ ./a.out

Enter the string:abc

Wrote3 bytes
Reverse string::cba
Read3 bytes:abc[soetcomp@localhost ~]$*/


Comments