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



<vladimir.plotnikov@xxxxxxxxx> wrote in message
news:1193405143.360453.36370@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm writing small C++ under Linux application with will be transfer
email from some IMAP server and put it to local database.
I have one problem:
I need to pass message to anti virus filter and spam filer.
When mail exceeded some limit, I my program is stopped on "waitpid()"
and wait when spamc script ends. but this script never ends. From
other side, another filter (anti virus) working fine on same code.

If the child consumes all input before producing any output, the parent
should write all the input, read the output until EOF, and only then call
wait(). Using the code you provided, if there is "too much" output, the
child will block in write() before it can exit, and the parent is waiting
for it to exit before calling read(): this is a deadlock.

If the child may produce output before consuming all input, you will have to
do something different (more complicated). You could use non-blocking IO and
select()/poll() or equivalent in the parent to move data between the
processes whenever possible (again, calling wait() only after you have read
EOF from the child). Or you could use a third process with some more
"plumbing" to create something like a shell pipeline. That is, after
creating the first child process (in which you exec), fork() again in the
parent to create another child which will read the output from the first
child.

HTH,
Alex


.



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)