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

From: Nadeem Abdul Hamid (nadeem at acm.org)
Date: Sat Sep 24 21:47:32 EDT 2011

That helps, thanks. Is there some way to flush the output port of the
process from Racket? Because it still hangs if I try to read before
writing to stdin:

#lang racket
(define-values (p stdout stdin stderr)
  (subprocess #f #f #f "./test"))
(read-line stdout 'any)   ; should read "Hello World\n"
; hangs ^^^^^
(display "123\n" stdin)
(flush-output stdin)
(port->string stdout)

This hangs even with the printf("Hello World\n") outputting a newline.
But if I add fflush(stdout); in the C program after the printf it
works.



On Sat, Sep 24, 2011 at 9:25 PM, Carl Eastlund <cce at ccs.neu.edu> wrote:
> 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.