(Sorry for the crossposting, I didn't know what newsgroup was most
appropriate for this question. Followup has been set to "poster", and
please reply per email. Also sorry if this appears more than one time, my
newsreader rejected my posting the first time. Thank you very much.)
I have encountered some rather strange behaviour of setbuf(3) on a HP 705,
HP-UX 8.07, standard HP C-compiler. I'm writing a plain vanilla filter
application, in which a process creates two pipes, then forks off a child,
and the child execs another program after connecting its stdin, stdout and
stderr to the pipes created just before. Parent and child then
communicate via the pipes.
Now the exec'd program is an interactive application, which writes a few
bytes to stdout, then waits for user input, then again writes a little
bit, and reads again, etc. And, since stdout is buffered, the few bytes
usually remain in the pipe, and the parent process doesn't get any input
from the pipe. This behaviour is inacceptable for my application, and so
I put a "setbuf(stdout,NULL)" into the child process, right after
reconnecting stdout to the pipe and immediately before the exec(). For
some unknown reason, this does *not* work. (I have also tried to execute
setbuf() directly on the pipe, with the same (negative) result.)
What does work, however, is placing a "setbuf(stdout,NULL)" at the start
of the exec'd program. In this case, everything works as expected, output
is available to the parent as soon as it is written by the child.
Unfortunately, the C-source for the exec'd program is generated
automatically from a Turbo Pascal program with a Pascal-To-C converter,
and is written and maintained by another person (my boss :-), which is
also the reason for the program not to contain a single call to
fflush(stdout). So I can't rely on a specific behaviour of the program, I
have to use it as is. (Even if I could safely change the program, I would
consider this a quite unsatisfying kludge.)
Can anyone tell me why setbuf() works "after" the exec(), but not before?
Does exec() change the file's buffers? Bug? Feature? RTFM? (which FM? :-)
Thanks a lot in advance for any help and hints (email please),
MfG
HH
--
- Technical University of Vienna, Austria // Institute for Computer Graphics -
- Life would be so much easier if we could just look at the source code. -