fork, exec, and pipes

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

fork, exec, and pipes

 
0
  #1
May 25th, 2008
A some point in my program, it is necessary for me to start a child process (Im on linux, btw, ubuntu gutsy). The process streams DV to stdout, but can be controlled in an "interactive mode" through commands to stdin.

How does one establish streams going both ways? If you use the good ol C libs, you can use popen (but that is only one way commication) or you can use pipe(). My understanding is that when you call exec(), all existing streams are closed, and all of your local variables destroyed. And Im pretty sure (please correct if wrong) that even though a file descriptor is represented as an interger, passing that integer to the child process would be worthless.

How do I create two way communication with a newly fork'd and exec'd process that ties back to the parent? (I would much rather have a c++ solution, but a C solution would work just as well)

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: fork, exec, and pipes

 
0
  #2
May 25th, 2008
Link.

Enjoy!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: fork, exec, and pipes

 
0
  #3
May 27th, 2008
The example was great. Now I have to ask, how can I use this in c++?
C++ (interfaces) do not directly deal with file descriptors as far as I can tell.

If I use your example to open up 4 file descriptors, dup2 them to the appropriate stdin/stdout, can I then use plain C++ <iostream> classes to interface with cin/cout?

Does using "unistd.h" and <iostream> cause any conflicts (such as <iostream> redefining cin and cout after I have already configured them the way I want using unistd.h.

Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: fork, exec, and pipes

 
0
  #4
May 27th, 2008
Accidentally posted twice, sorry
Last edited by petzoldt01; May 27th, 2008 at 6:32 pm. Reason: posted same thing twice
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: fork, exec, and pipes

 
0
  #5
May 27th, 2008
The example was great. Now I have to ask, how can I use this in c++?
C++ (interfaces) do not directly deal with file descriptors as far as I can tell.

If I use your example to open up 4 file descriptors, dup2 them to the appropriate stdin/stdout, can I then use plain C++ <iostream> classes to interface with cin/cout?

Does using "unistd.h" and <iostream> cause any conflicts (such as <iostream> redefining cin and cout after I have already configured them the way I want using unistd.h.

Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: fork, exec, and pipes

 
0
  #6
May 27th, 2008
Unfortunately the standard decided that such lowly things as file descriptors are too device-dependent, so there is no standard way to do that.

That said, many compiler systems provide a way to create an iostream connected to a file descriptor. Check your compiler documentation.

If you are up to it, a better solution would be to derive an iostream class that does it. Since this is a common problem, solutions by other people exist.
http://www.josuttis.com/cppcode/fdstream.hpp.html (very compact and simple)
http://www.boost.org/doc/libs/1_35_0...doc/index.html (very powerful)

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 45
Reputation: petzoldt01 is an unknown quantity at this point 
Solved Threads: 0
petzoldt01 petzoldt01 is offline Offline
Light Poster

Re: fork, exec, and pipes

 
0
  #7
May 28th, 2008
Those libraries were extremely helpful. I really had no idea what I was trying to get myself into.

So, with the example you gave me earlier, I create my four file descriptors. In the parent, I use the fdstream header you linked to to create streams using the parent side file descriptors. Now the child...

(right after the fork statement)
  1. if ( childpid == 0 ) /* in the child */
  2. {
  3. close(PARENT_WRITE);
  4. close(PARENT_READ);
  5.  
  6. dup2(CHILD_READ, 0); close(CHILD_READ);
  7. dup2(CHILD_WRITE, 1); close(CHILD_WRITE);

Correct me if I am wrong. At this point, I can now call exec(), and the file descriptors from my pipes will still be in place for std in/out on the CHILD PROCESS.

If not, how do I make sure the pipe still exists after I make the exec() call?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: fork, exec, and pipes

 
1
  #8
May 28th, 2008
You are correct. The process file table and the current working directory are not modified by exec(), so the new executable inherits it all.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC