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


1:  #include"iostream" 

2:  #include"cstring" 

3:  #include"stdlib.h" 

4:  #include"unistd.h" 

5:  #include"sys/types.h" 

6:  using namespace std; 

7:  int main() 

8:  { 

9:    int   fd[2]; 

10:    int   childpid; 

11:    char  string[] = "D.Y PATIL ENGG COLLEGE"; 

12:    char  readbuffer1[80], readbuffer2[80]; 

13:    pipe(fd); 

14:    if((childpid = fork()) == -1) 

15:    { 

16:      cout<<"\nfork() is not executed successfully"; 

17:      exit(1); 

18:    } 

19:    if(childpid == 0) 

20:    { 

21:      cout<<"\n************************************************************"; 

22:      cout<<"\n    IPLEMENTATION OF PIPE         "; 

23:      cout<<"\n************************************************************"; 

24:      cout<<"\nPid of child process: "<<getpid(); 

25:      cout<<"\nString passed through one end of pipe: "<<string; 

26:      close(fd[0]); 

27:      write(fd[1], string, (strlen(string)+1)); 

28:      cout<<endl; 

29:      exit(0); 

30:      } 

31:      else 

32:    { 

33:      cout<<"\n\nPid of parent process: "<<getpid(); 

34:      close(fd[1]); 

35:      read(fd[0], readbuffer1, sizeof(readbuffer1)); 

36:      cout<<"\nString accepted from other end of pipe: "<<readbuffer1; 

37:      for(int i=0, n = strlen(readbuffer1) - 1; i <= strlen(readbuffer1); i++, n--) 

38:      { 

39:        readbuffer2[i] = readbuffer1[n]; 

40:      } 

41:      cout<<"\nReversed string: "<<readbuffer2<<endl; 

42:    } 

43:    return(0); 

44:  } 

2 Comments

  1. can i solve this problem without using system call??
    If yes then please post source code

    ReplyDelete
  2. First, professionals are able to access the latest equipment and boast of up-to-date training. This enables them to perform their tasks without creating any problems. Whether you want to improve indoor air quality and stop allergies or reduce energy costs caused by inefficiency. Duct Replacement Services Melbourne

    ReplyDelete
Previous Post Next Post