Re: stdout redirected to a process's own stdin
From: Marc Rochkind (rochkind_at_basepath.com)
Date: 09/04/03
- Next message: Rich Teer: "Re: vi command - cursor movement"
- Previous message: lundpalli: "Re: Are there Win32 functions that work on Unix also????"
- In reply to: Jason Creighton: "stdout redirected to a process's own stdin"
- Next in thread: Marc Rochkind: "Re: stdout redirected to a process's own stdin"
- Reply: Marc Rochkind: "Re: stdout redirected to a process's own stdin"
- Reply: Jason Creighton: "Re: stdout redirected to a process's own stdin"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
>
- Next message: Rich Teer: "Re: vi command - cursor movement"
- Previous message: lundpalli: "Re: Are there Win32 functions that work on Unix also????"
- In reply to: Jason Creighton: "stdout redirected to a process's own stdin"
- Next in thread: Marc Rochkind: "Re: stdout redirected to a process's own stdin"
- Reply: Marc Rochkind: "Re: stdout redirected to a process's own stdin"
- Reply: Jason Creighton: "Re: stdout redirected to a process's own stdin"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|