[racket] Why does this hang (reading subprocess stdout)?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Sat Sep 24 21:25:05 EDT 2011

On Sat, Sep 24, 2011 at 9:19 PM, Nadeem Abdul Hamid <nadeem at acm.org> wrote:
> I'm not sure if there's something I'm missing, but I'm trying to write
> a script that writes to a subprocess' stdin and then reads its stdout.
> The program being executed (via 'subprocess') is this C program:
>
> /* test.c */
> #include <stdio.h>
> int main() {
>  int d;
>  printf("Hello World!");
>  scanf("%d", &d);
>  printf("\nDone %d\n", d);
>  return 0;
> }
>
> My Racket script is:
>
> #lang racket
> (define-values (p stdout stdin stderr)
>  (subprocess #f #f #f "./test"))
> (display "123\n" stdin)
> (port->string stdout)
>
> And it hangs on reading stdout, and (char-ready? stdout) produces #f.
>
> ***********************************
>
> The following program that doesn't have the scanf doesn't cause the
> Racket script to hang:
>
> #include <stdio.h>
>
> int main() {
>  int d;
>  printf("Hello World!");
>  return 0;
> }
>
> produces
> "Hello World!"
> when the same Racket program above is run.
>
> This is on Mac OS X Lion. I compile the C program with "gcc test.c -o test".
>
> I appreciate any help!
>
> --- nadeem

Have you tried using flush-output-port on stdin after the call to
display?  Looks like your output may be buffered and not actually sent
across the pipe yet.

--Carl



Posted on the users mailing list.