Re: stdout redirected to a process's own stdin

From: Marc Rochkind (rochkind_at_basepath.com)
Date: 09/04/03


Date: Wed, 03 Sep 2003 20:54:35 -0600

On Wed, 3 Sep 2003 14:38:48 -0600, Jason Creighton
<androflux@softhome.net.remove.to.reply> wrote:

[snip]

> #include <stdio.h>
>
> int main() {
> int chr;
> puts("Hello world!");

Probably does not put anything in the pipe because output is buffered.

> while((chr = getchar()) != EOF) {

Probably is waiting here for some input from the pipe.

> chr = (chr + 1) % 255; putc(chr, stdout);
> putc(chr, stderr);
> }
> return 0;
> }
>
> My thinking was that the puts("Hello world!") would get the loop started
> (ie, provide input for getchar() ) and then I could monitor what's it's
> doing from the output on stderr.
>

OK thinking... I wouldn't worry about this becoming a bad habit, but would
continue to do it as you feel the need. ;-)

> however:
>
> ~/prog/c$ ./loop adder
>
> ...hangs until I press control-C.
>
> I suspect this is due to a misunderstanding of how pipes work. Where did
> I go wrong?

Not really. The UNIX system has no way of knowing about any
misunderstanding you might have had. Most likely the system is just
behaving as directed by the program. ;-)

All kidding aside... turn off the output buffering with setbuf or flush the
buffer after the puts with a call to fflush.

(This is such a classic error that I put it in a book I wrote about 18
years ago.)

>
> Jason Creighton
>



Relevant Pages

  • Re: Design help...an explosion of interfaces......
    ... pipe is buffered are not; it is just loading or extracting from the pipe ... and call the right method with the right Pipe subclass instance. ... who understands the buffering context instantiate the relationship and ... the type attribute has a different semantics than the class ...
    (comp.object)
  • SUMMARY: Tru64 pipe buffer size
    ... The problem was that the application detected whether its stdout was going to file (pipe) or term, and did buffering in the case of a file. ... there was an easier way around the problem in our particular situation: Pipe the output via the tee command. ... Subject: Tru64 pipe buffer size ... We have a third-party tool which writes line-by-line to terminal device, but when its output is piped it seems to be buffering the data in chunks of approx 4KB at a guess. ...
    (Tru64-UNIX-Managers)
  • Re: fwrite over a pipe
    ... fprint or fwrite to output to the parent-to-child command pipeline, ... it might not be on the actual pipe when you do a read of the child." ... The fwrite and fprintf functions use buffered I/O. ... The buffering of these functions can be modified (or even turned ...
    (comp.unix.programmer)
  • Re: circular buffering using substr() ?
    ... Can I use substr() to do circular buffering? ... stream from a pipe, which I need to send out on another pipe, but ... $offset); # OUTPIPE is non-blocking ...
    (comp.lang.perl.misc)
  • Re: pipe buffering
    ... read-end of pipe, ... the problem is with buffering, is there a way to disable buffering on pipe? ... are you using fflush in the child process? ...
    (microsoft.public.vc.language)