Q: Process A accepts a character string and Process B inverses the string. Pipe is used to establish communication between A and B processes using C++ & Python
Note: The process communication in this example takes place through name pipes. Unnamed in c++ simpler and can be found on Internet. Or better still just type info pipe on terminal you will get the entire code.
[te@dbsl2 3101]$
Received:This is the string to be reversed
The reversed string is
desrever eb ot gnirts eht si sihT
[te@dbsl2 3101]$
Enter the string: reverseme
Reversed String is emesrever
Enter String: reversemereversed string is emesrever
Note: The process communication in this example takes place through name pipes. Unnamed in c++ simpler and can be found on Internet. Or better still just type info pipe on terminal you will get the entire code.
Code
Reader.cpp
#include <fcntl.h> #include <iostream> #include <sys/stat.h> #include <unistd.h> #include <string> #include <string.h> #define MAX_BUF 1024 using namespace std; char* strrev(char *str){ int i = strlen(str)-1,j=0; char ch; while(i>j) { ch = str[i]; str[i]= str[j]; str[j] = ch; i--; j++; } return str; } int main() { int fd; char *myfifo = "/tmp/fifopipe"; char buf[MAX_BUF]; /* open, read, and display the message from the FIFO */ fd = open(myfifo, O_RDONLY); read(fd, buf, MAX_BUF); cout<<"Received:"<< buf<<endl; cout<<"The reversed string is \n"<<strrev(buf)<<endl; close(fd); return 0; }
Writer.cpp
#include <fcntl.h> #include <iostream> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string> #include <string.h> using namespace std; int main() { int fd; char * myfifo = "/tmp/fifopipe"; /* create the FIFO (named pipe) */ mkfifo(myfifo, 0666); /* write message to the FIFO */ fd = open(myfifo, O_WRONLY); char *msg; msg="This is the string to be reversed"; write(fd, msg, strlen(msg)+1); close(fd); /* remove the FIFO */ unlink(myfifo); return 0; }
Output
On two seperate terminal tabsFirst
[te@dbsl2 3101]$ ./writer[te@dbsl2 3101]$
Second
[te@dbsl2 3101]$ ./readerReceived:This is the string to be reversed
The reversed string is
desrever eb ot gnirts eht si sihT
[te@dbsl2 3101]$
Python
In python we can use ipc using fifo as followsWriter
import os path= "/home/hulk/myProgram.fifo" os.mkfifo(path) fifo=open(path,'w') string=raw_input("Enter String to be reversed:\t ") fifo.write(string) fifo.close()
Reader
import os import sys path= "/home/hulk/myProgram.fifo" fifo=open(path,'r') str=fifo.read() revstr=str[::-1] print "The Reversed String is",revstr fifo.close()
Output
Writer
hulk@unknown:~$ python writer.pyEnter the string: reverseme
Reader
hulk@unknown:~$ python reader.pyReversed String is emesrever
Unnamed pipe
import os,sys fdr,fdw=os.pipe(); procid=os.fork(); if pid: #this is the parent process section it will read os.close(fdw) print "reversed string is ", os.fdopen(fdr).read()[::-1] sys.exit(0) else: #this is the child process section it will write os.close(fdr) os.fdopen(fdw,'w').write(raw_input("Enter String: ")) sys.exit(0)
Output
hulk@unknown:~$ python pipe.pyEnter String: reversemereversed string is emesrever
No comments:
Post a Comment