Wednesday, October 29, 2014

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++.

Problem Statement:

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++.

PROGRAM

Unnamed Pipe

#!/usr/bin/python
import os,sys

class npipe:
        def unnamed(self):
                fdr,fdw=os.pipe()
                procid=os.fork()
                if procid:
                    os.close(fdw)
                    fdr=os.fdopen(fdr)
                    str=fdr.read()
                    str=str[::-1]
                    print "reversed string: ",str
                    sys.exit(0)
                else:
                    os.close(fdr) 
                    str1=raw_input("Enter string: ")
                    fdw=os.fdopen(fdw,'w')
                    fdw.write(str1)
                    fdw.close()
                    sys.exit(0)

ob=npipe()
ob.unnamed()

Named Pipe

n_1.py

#!/usr/bin/python
import os
import sys

class a5_1:
        fd=0
        pipename="pipe3"

        def create_pipe(self):
                os.mkfifo(self.pipename)

        def write_pipe(self):
                self.fd=open(self.pipename,'w')
                self.fd.write("1")
                self.fd.close()
                str=raw_input("Enter string: ")
                self.fd=open(self.pipename,'w')
                self.fd.write(str)
                self.fd.close()

obj1=a5_1()
obj1.create_pipe()

while True:
    print "1.Enter string  2.Exit"
    choice=raw_input("Enter choice: ")
    if choice=='1':
        obj1.write_pipe()
    else:
        obj1.fd=open(obj1.pipename,'w')
        obj1.fd.write("2")
        obj1.fd.close()
        os.unlink(obj1.pipename)

        break;

n_2.py

#!/usr/bin/python
import os
import sys

class a5_2:
        fd=0
        pipename="pipe3"
        def read_pipe(self):
                self.fd=open(self.pipename,'r')
                stri=self.fd.read()
                revstr=stri[::-1]
                print "reveresed string: ",revstr
                self.fd.close()

obj2=a5_2()
while True:
    obj2.fd=open(obj2.pipename,'r')
    choice=obj2.fd.read()
    if choice=='1':
        obj2.read_pipe()
    else:
        break


No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...