Re: newbie question about fork/pipe/stdin/stdout
- From: "Alex Fraser" <me@xxxxxxxxxxx>
- Date: Tue, 30 Oct 2007 19:26:43 -0000
<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
.
- References:
- Re: newbie question about fork/pipe/stdin/stdout
- From: Alex Fraser
- Re: newbie question about fork/pipe/stdin/stdout
- From: vladimir . plotnikov
- Re: newbie question about fork/pipe/stdin/stdout
- From: David Schwartz
- Re: newbie question about fork/pipe/stdin/stdout
- From: vladimir . plotnikov
- Re: newbie question about fork/pipe/stdin/stdout
- Prev by Date: Re: Removing all files inside a directory in Linux using an API call from a C Code
- Next by Date: Re: Reclaiming locks
- Previous by thread: Re: newbie question about fork/pipe/stdin/stdout
- Next by thread: web server design
- Index(es):
Relevant Pages
|