TCL坚持下去

TCL hang on gets

我有一个 parent C 进程创建了一个 child tcl 进程和 re-directs child 的 stdin/stdout 来与 parent。它似乎工作正常,但有一部分 child 永远不会收到 parent 收到的内容。如果有人能发现导致此问题的错误,我将不胜感激。

Parent发送代码:

fprintf(write_to, "%s",outmsg.data); //Received by child
bzero ((char *) &inmsg, sizeof (inmsg));
getmsg (sock, &inmsg);
if (ntohl (inmsg.type) != MATCH)
  protocol_error (MATCH, &inmsg);
strncpy (opphandle, inmsg.data, maxSize);
opphandle[maxSize] = '[=10=]';
fprintf (write_to,"%s",opphandle); //Received by child
fprintf (write_to,"%s",inmsg.board); //Not Received by child

inmsg 中的 board 和 data 字段都是 char[] 类型。

Child接收码:

set handle [gets stdin] //Received
set ohandle [gets stdin]//Received
set myShape [gets stdin]//Not received

注意,当用 ctrl+C 杀死 parent 时,child 认为它收到了 myShap 的输入,然后用 myShape 执行 ctrl+C。

同样,child 的标准输入和标准输出已被重定向,因此标准输入来自 parent,标准输出转到 parent。

gets 命令等待换行。

很可能 outmsg.data 和 opphandle 都以换行结尾("\n""\r""\r\n")。它可以解释为什么前两个 gets 有效。

检查 inmsg.board 是否以换行结束。如果没有,你可以简单地做:

fprintf (write_to,"%s\n",inmsg.board);

为了健壮性,最好在发送前去除尾随换行符的字符串。然后像上面的例子一样在 fprintf() 中再次添加换行符。这是为了避免额外的换行符意外导致您的 tcl 脚本将空行读取为数据的情况。

另一种方法是在 tcl 脚本中实现适当的 scanner/parser 以忽略空行。