Re: newbie question about fork/pipe/stdin/stdout



<vladimir.plotnikov@xxxxxxxxx> wrote in message
news:1193649642.329918.170420@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hello agin,
Thank you for replies ;-)

I changed my code to next:
....
fcntl(pipeStdout[0], F_SETFD, fcntl(pipeStdout[0], F_GETFD) |
O_NONBLOCK);
fcntl(pipeStdin[1], F_SETFD, fcntl(pipeStdin[1], F_GETFD) |
O_NONBLOCK);

You must use F_GETFL/F_SETFL here.

....
stream = fdopen (pipeStdin[1], "w");
streamIn = fdopen (pipeStdout[0], "r");

You can't (reliably, at least) use stdio with descriptors set to
non-blocking mode; use read() and write().

[snip rest of code]
Where I wrong again?

I would use a loop which does something like this (I've ignored error
conditions etc which obscure the basic logic):

1. Call select() to wait until it reports pipeStdout[0] is readable or
pipeStdin[1] is writeable. (The latter only if you still have data to
write to the child.)
2a. If pipeStdout[0] is readable, call read().
2b. If read() returns 0 (indicating EOF), exit the loop.
2c. Do whatever you need to with the data just read.
3a. If you have data to write to the child and pipeStdin[1] is writeable,
call write() to try to write some more data. For efficiency, try to
write as much data as you have available.
3b. If you succeed in writing the last byte, close pipeStdin[1] so the
child will see EOF, and make it so that in #1 you no longer check the
status of pipeStdin[1] (for obvious reasons).

After you exit the loop, call wait() to wait for the child process to exit
(if it hasn't already) and collect its status.

Alex


.



Relevant Pages

  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • TCP Server+perl
    ... Client each time a new connection is established at the Socket. ... each child completes processing it closes/ends. ... # Main loop control variable ... You exit the ...
    (comp.lang.perl.misc)
  • Re: Infinite Loops and Explicit Exits
    ... > CS> One of these proposals relaxes the current restriction that an EXIT ... > termination of the loop is not visible at that point. ... > terminating condition is visible in that context. ... > You now want to allow this remote procedure, ...
    (comp.lang.cobol)
  • Re: Houston-related rants was Re: forced merges/inside lane merges/AASHTO "tapered merges"
    ... >> Are you sure about the North Loop and I-45? ... >> in particular the westbound exit for Fondren/Gessner. ... How could HCTRA put an exit ramp for eastbound ... and the West Houston Center Blvd exit still shows Old Westheimer ...
    (misc.transport.road)
  • Re: Exit Do
    ... I've been instructed by a dot net / asp programmer that Exit Do is ... I am using nested loops and need to exit out of one loop into ... If Some Reason to stop loop then Exit Do ... Code Stage B ...
    (microsoft.public.scripting.vbscript)