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

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

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


Posted on the users mailing list.