Link to the example… on wiki.tcl.tk
There is an example here for extending tcl through the use of an executable module that communicates through pipes. (Located in the section marked Executable Modules (EM))
I have compiled the program using Ubuntu and Windows XP. When I try to run the script that tests the modules – they both hang.
Whats missing from the example?
Looks like the example is missing out handling of flushing of the output side of the pipes. The data’s being buffered up in OS buffers (waiting for a few kilobytes to be built up) instead of actually being sent immediately to the other process. Note that this buffering only happens when the output is being directed to something other than a terminal, so you won’t see it when interactively testing. (Its also not important if lots of data is being written, when the improved efficiency of having that buffering is a winner.)
On the C side, add this line at the top of the
mainfunction:On the Tcl side, add this immediately after the starting of the subprogram:
The C side can be also done by using
fflushafter everyprintf. If you’re stuck with a real C program that you don’t have access to the source of, you can still make progress by wrapping the the whole program with theunbufferprogram (actually a Tcl script that uses magic with Expect to make the subprocess think it’s talking to a terminal). The one down-side ofunbufferis that it uses a virtual terminal, which comes from a far more restricted pool of resources than plain old process IDs (let alone pipes/file descriptors).