1

I have a C program which spawns a pthread to act as an interactive terminal: reading lines from stdin & acting upon them. The program acts as a kind of shell, fork-ing off processes; each process so created has its stdin re-directed using a call to freopen() before using exec to load the new executable.

Before the interactive thread is started, all works fine. Once it has started (or, more specifically, whenever it is waiting for input), calls to freopen() hang. Is there some way to avoid this problem?

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101

1 Answers1

1

The solution that is working for me can be found in R.'s answer to Is close/fclose on stdin guaranteed to be correct?

Basically, the idea is to open the file you want to redirect to, duplicate that to stdin, and then close the file description just opened.

Community
  • 1
  • 1
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101