UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » read child's output after a fork()

#1 2007-10-20 01:22 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: read child's output after a fork()

Hi,

I've looked around on the i-net, and found a lot of topics and questions about it. But none of them where in C.

Let's say, I've this piece of code:

int pid; // of course.. (do you write that with or without a space ? )

pid = fork(); // give the integer pid the value of the Process ID of my chield. or 0 if this is the chield.
if(fork() == 0) { // chield
    //blah blah, let's do some chield code. Wich I wan't to be executed
}


The fork isn't there for nothing. So.. How can I read the data wich collects my chield ?
I heard about pipes (popen()), but what I see about that is that it executes a command as well.

So. Is there someone here who's experienced in this, and can explain me (maybe with some little code) how it works ?

Offline

#2 2007-10-20 06:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: read child's output after a fork()

Offline

#3 2007-10-20 07:13 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: read child's output after a fork()

Oh, too bad.. I thought it would be easy, but I see that I'll need a chunck of code :(

Offline

#4 2007-10-20 07:25 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: read child's output after a fork()

The first question that pops up in these cases is why you are forking a child.
If you know the why, it's easier to choose which IPC method to use which is
most suitable for this case. If in the end you have more code handling the
child and data communication than doing real work, you might want to
reconsider if using childs was a good idea or not. fork + exec* is a classical
case where you don't have much alternatives.

Offline

#5 2007-10-20 07:28 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: read child's output after a fork()

I do need it .
because if I don't, some processes will stop until others are done.
I really need it. My code is "already" >1000 lines. Not much, but more as the pipe communication. I hopen it would only be:

popen or something
for { fread }

for my "mother proces".

basicly: mummie only needs to read, and chield only needs to write ..

Offline

#6 2007-10-21 05:46 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: read child's output after a fork()

I'm not sure what you think is so complicated...  It's really not MUCH more complex
than your pseudocode there...  Just a pipe() call to create the pipe FDs, then you're
already doing the fork() now, so that's no additional complexity; then, just close() one
end in both parent and child, then write() in the child and read() in the parent...  It's
really not that complicated...  Maybe you're seeing all the dup2() stuff and being put
off by that?  That's only necessary if you want to force the pipe FDs to replace stdin
and/or stdout, which is generally only done when exec*()'ing another process...

Offline

  • Index
  • » C
  • » read child's output after a fork()

Board footer

Powered by FluxBB