Remembering Tcl: Problems with C and Tcl communicated via pipelines

Remembering Tcl: Problems with C and Tcl communicated via pipelines

Post by montgomme » Thu, 05 Feb 2004 23:10:10


I am now remembering TCL and I have came across with a problem when
doing something I used to do.

My purpose is to implement a console-like GUI to present in a listbox
outputs from another program written in c and allow to command orders
via an entry. All the communication between both programs will be via
pipeline

As a first approximation, I tried several easy examples and I get
stucked with these two programs (see below).

When executing tclprogram2.tcl I get Done1 and Done2 and nothing more:
i must end it with Ctrl-C. However, if I change Hello3 lines before
for loop, the program run smoothly (even without changing cprogram2),
but this is not what I need, as I must see what the cprogram writes
before giving an answer.

Any ideas of how solve it?

----------------------------------------------------------
tclprogram2.tcl:
----------------------------------------------------------
#!/bin/sh
# the next line restarts using wish \
exec wish8.1 "$0" "$@"

set ProgramPid [ open "| ./cprogram2" "r+" ]

set GrepResult [exec ps -ef | grep cprogram ]
puts stdout $GrepResult

puts $ProgramPid "Hello"
flush $ProgramPid
puts stdout "Done1"

puts $ProgramPid "Hello2"
flush $ProgramPid
puts stdout "Done2"

foreach i { 0 1 2 3 4 5 6 } {
set SalidaStatus [gets $ProgramPid Salida]
puts stdout "$Salida $SalidaStatus"
}

puts $ProgramPid "Hello3"
flush $ProgramPid
puts stdout "Done3"

set SalidaStatus [gets $ProgramPid Salida]
puts stdout "$Salida $SalidaStatus"


----------------------------------------------------------
cprogram2.c:
----------------------------------------------------------
#include <stdio.h>

#define MAX_NUMBER 5
#define DELAY 100000

int main (void)
{
int i, j;
char orden_tcl[100] = "prueba";
char orden_tcl2[100] = "prueba";
char orden_tcl3[100] = "prueba";

scanf("%s", orden_tcl);
scanf("%s", orden_tcl2);
printf("Tcl ha dicho: %s y %s\n", orden_tcl, orden_tcl2);

for (i= 0; i < MAX_NUMBER; i++)
{
printf ("%d\n",i);
}

printf ("Fin delay\n");

scanf("%s", orden_tcl3);
printf("Tcl ha dicho: %s \n", orden_tcl3);
}
 
 
 

Remembering Tcl: Problems with C and Tcl communicated via pipelines

Post by Bruce Hart » Fri, 06 Feb 2004 00:37:27


make sure that the C program is flushing it's output after every line
and add "fconfigure $ProgramPid -buffering line" to you tcl code as well.

Bruce

 
 
 

Remembering Tcl: Problems with C and Tcl communicated via pipelines

Post by montgomme » Fri, 06 Feb 2004 17:21:54


How can I do c program flushes its output? I think finishing every
printf with \n is enough to flush output.

Jorge
 
 
 

Remembering Tcl: Problems with C and Tcl communicated via pipelines

Post by David Grav » Fri, 06 Feb 2004 17:35:17


not exactly. It depends on the mode the stream is in and the
implementation itself.

fflush(stdout);
--
David Gravereaux < XXXX@XXXXX.COM >
[species: human; planet: earth,milkyway(western spiral arm),alpha sector]
 
 
 

Remembering Tcl: Problems with C and Tcl communicated via pipelines

Post by Bruce Hart » Sat, 07 Feb 2004 00:40:03


Generally speaking stdout is line buffered (so \n will cause a flush)
and stderr is unbuffered. so probably all you need to do is make sure
in your tcl script to have the input line buffered & you're good to go.
If it still isn't working, you can explicitly flushing after each write
with fflush(stdout) or use setvbuf to alter buffering on the file

Bruce