简体   繁体   English

TCL挂上

[英]TCL hang on gets

I have a parent C process that creates a child tcl process and re-directs the stdin/stdout of the child to interact with the parent. 我有一个父C进程,该进程创建一个子tcl进程并重定向该子的stdin / stdout与父进行交互。 It seems to work correctly but there is a part where the child never receives what the parent is receiving. 它似乎可以正常工作,但是在某些情况下,孩子永远不会收到父母正在收到的东西。 If any one can spot the error that causes this I would appreciate it. 如果有人能发现导致此错误的错误,我将不胜感激。

Parent sending code: 家长发送代码:

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] = '\0';
fprintf (write_to,"%s",opphandle); //Received by child
fprintf (write_to,"%s",inmsg.board); //Not Received by child

both board and data fields in inmsg are of type char[]. inmsg中的board和data字段均为char []类型。

Child recieving code: 儿童接收代码:

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

Note, when the parent is killed with ctrl+C the child thinks it received input for myShap and then executes with myShape being ctrl+C. 请注意,当父级被ctrl + C杀死时,孩子认为其已收到myShap的输入,然后以ctrl + C的myShape身份执行。

Again, stdin and stdout of the child have been redirected so that stdin comes from the parent and stdout goes to the parent. 再次,子级的stdin和stdout已被重定向,以便stdin来自父级,而stdout转到父级。

The gets command waits for newline. gets命令等待换行符。

It's probable that both outmsg.data and opphandle ends in newlines ( "\\n" or "\\r" or "\\r\\n" ). outmsg.data和opphandle都可能以换行符结尾( "\\n""\\r""\\r\\n" )。 It would explain why the first two gets works. 它可以解释为什么前两个方法gets工作。

Check to see if inmsg.board ends in a newline. 检查inmsg.board是否以换行符结尾。 If not, you can simply do: 如果没有,您可以执行以下操作:

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

For robustness, it's better to strip strings of trailing newlines before sending. 为了提高鲁棒性,最好在发送前去除尾随换行符的字符串。 Then add the newline again in the fprintf() like the example above. 然后像上面的示例一样,在fprintf()再次添加换行符。 This is to avoid cases where extra newlines accidentally causing your tcl script to read an empty line as data. 这是为了避免多余的换行符意外导致tcl脚本读取空行作为数据的情况。

An alternative is to implement a proper scanner/parser in the tcl script to ignore empty lines. 一种替代方法是在tcl脚本中实现适当的扫描仪/解析器,以忽略空行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM