pty主端输出奇怪

问题描述

我正在开发一个远程Shell程序,我用forkpty()创建了一个pty。从跟随者端到领导者端的写作按预期工作,但是当我开始尝试在跟随者端进行阅读时,我在领导者端得到了奇怪的输出

我明白了:

$
socket->pty: interesting
pty->socket: ^@^@^@^@^@^@^@^@^@^@▒▒ɖ^BE<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X▒߰^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒     ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒԰^@^@=L@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^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^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒԰^@^@=L@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@

我认为它可能与调试输出有关,因此我将其禁用,但是:

pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^C^@^@▒^E^@^@▒        ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@
socket->pty: ?
pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒      ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@
socket->pty: what
pty->socket: s^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A▒x▒^@^@@`P▒^@^@^A▒▒▒▒▒▒▒^H▒x▒^@^@▒▒x▒^@^@^@^@^@^@^@^@^@^@▒     ^_▒^AM<^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^X"W▒^@^@^@^@^@^@^@^@^@^@^@^@^@^@ ^@^@^@^@^@^@^@^C^@^@▒^E^@^@▒      ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@▒   ^@^@^@^@^@^@^@^@^@^@^@^A^@^@@^@^@^@^@^@^@^@@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@@^@^@^@^@^@^K^@^@^@^@^@^@^@▒▒▒^@^@^@^@^@▒^@^@^@^@^@^@^@▒▒C▒^@^@▒L@^@^@^@^@^@^@^@^@

它似乎在重复,但我不知道它是什么。

跟随者的代码

fprintf(stdout,"$");
fgets(input_buff,buff_len,stdin);

领导者代码

chars_read = read(leader_fd,&buff,buff_len - 1);
if (chars_read > 0) {
    buff[chars_read] = '\0';
    printf("pty->socket: %s\n",buff);
    write(socketfd,buff,chars_read);
}

chars_read = read(socketfd,buff_len - 1);
if (chars_read > 0) {
    buff[chars_read] = '\0';
    printf("socket->pty: %s\n" buff);
    write(leader_fd,chars_read);
}

当我从关注者端删除fgets时,我的输出将变为:

socket->pty: what
pty->socket: .txt: No such file or directory
$56152 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56153 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56154 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56155 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56156 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56157 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56158 (st
socket->pty: why?
pty->socket: arted): cat hello.txt
cat: hello.txt: No such file or directory
$56159 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56160 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56161 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56162 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56163 (started): cat hello.txt
cat: hello.txt: No such file or directory
$56164 (started): cat hello.txt

为什么在追随者方面的阅读会导致在领导者方面的阅读混乱?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)