Jump to content

2 way Pipes in C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
How to pass values from main.cpp to child.cpp via Pipes.....Also making sure that its a 2 way communication...main program sends a data(received by child.cpp) and child sends back the data to the main.cpp after working on it...Please help....find below the main function of what I have done so far:
child.cpp
int main( int argc, char *argv[])
{
  int cpipes[2];    
  pipe(cpipes); //create pipe
  char buffer[4096];    
  int choice = atoi(argv[1]);
  int num=0;
  
  close(cpipes[1]);
  read(cpipes[0], buffer, sizeof(buffer));							   
  cout<<" Received at converter: "<<buffer<<endl; 
}
-----------------------------
main.cpp
int main()
{
  int pipes[2];
  char number[4096]; //storage for 32 bits
  char options[3];   
 
  pipe (pipes); //create pipe
 
  if (fork() == 0) 
    {
      wait(NULL);  // wait till child writes	
    }
  else{		        
	  menu();	 
	  sprintf(number, "%d", choice); //convert integer to characters	  	 
	  sprintf(options, "%d", choice); //convert integer to characters	  	 
	  write(pipes[1], number, strlen(number));	  
      cout << " Sent = " << number << endl;	    
      execl("./child", "child", options, NULL); //pass write end buffer as argument to child	  
    }
  return 0;
}

Edited by WingedPanther, 07 June 2009 - 05:31 PM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Can you give a few more details about what you're trying to do? Also, you haven't included #include's to be able to compile your code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
OOH! I just did this for my final project this year. What we did is this:

1) Parent opens a pipe
2) Parent forks
3) Child calls one of the exec* funtions to invoke the other program, passing the numeric value of the other end of the pipe on the command line.
4) Other program reads in values passed on the command line, converts them to file descriptors using atoi(), and voilĂ ! Pipe communication.

You'll need unistd.h and sys/types.h, if I remember correctly.

#4
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
Can you please forward me the basic programs for that....What Iam finding difficulty is that how to go about passing values from two programs via pipe....(not thru as arguments)....my header files for both programs is:

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>


Please help me out!!!!

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
You use read() and write(), just like with regular files. Alternatively you could call fdopen() on the file descriptors and use fprintf() and the like.

#6
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
My main.cpp is as follows:
///can I have a child(child.cpp) program that can read this pipe.


#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int number;

int main()
{
  int pipes[2];
  char number[4096]; 
 
  pipe (pipes); //create pipe
 
  if (fork() == 0) 
    {
      wait(NULL);  // wait till child writes	
    }
  else{		         
	  sprintf(number, "%d", choice); //convert integer to characters			 
	  write(pipes[1], number, strlen(number));	  
      cout << " Sent = " << number << endl;	    
      execl("./converter", "converter", number, NULL);
    }
  return 0;
}

Edited by WingedPanther, 08 June 2009 - 06:26 AM.
add code tags (the # button)


#7
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
Can you post up a simple program (code snippets) dat demonstrates it...please

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Not right now, I have a final exam tomorrow. But I'd be happy to post one Tuesday.

#9
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
//For example I want to read the pipe contents from a different file(.cpp) as this program is reading it in the same program.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

	int main()
	{

		int pid, pip[2];
		char buf[100];

		pipe(pip); 
		pid = fork();
		if (pid == 0)           
		{
			wait(NULL);  
		}
		else			
		{				
			write(pip[1],"Ready sent...",100);			
			execl("./test1", "test1",buf,NULL);
			read(pip[0],buf,100);
			
			cout<<"Read in main: "<<buf<<endl;
		}
        }

Edited by WingedPanther, 08 June 2009 - 06:27 AM.
add code tags (the # button)


#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Please wrap your code in code tags next time. It's the little pound sign button.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
Had a look at your code - it won't work. execl() doesn't execute the specified program and return. It replaces your program. It never returns. You'll get infinite forking until it fails.
sudo rm -rf /

#12
Rajneshwar

Rajneshwar

    Newbie

  • Members
  • Pip
  • 7 posts
so what's your suggestion for this...