简体   繁体   English

fgets 在 Windows 上从 bash 运行时导致 C 程序挂起,但在从 CMD 或 WSL 运行时正常工作

[英]fgets causes C program to hang when run from bash on Windows, but works correctly when run from CMD or WSL

I'm following a 'Build your own Lisp' tutorial to teach myself C, and came across some strange behavior related to fgets.我正在学习“构建你自己的 Lisp”教程来自学 C,并且遇到了一些与 fgets 相关的奇怪行为。 Here is a link to the Chapter I am working on: http://www.buildyourownlisp.com/chapter4_interactive_prompt#an_interactive_prompt这是我正在研究的章节的链接: http : //www.buildyourownlisp.com/chapter4_interactive_prompt#an_interactive_prompt

The code is a simple base for what will eventually be the REPL.该代码是最终将成为 REPL 的简单基础。 It just prints some info, and then starts a loop that gets and prints user input.它只是打印一些信息,然后启动一个循环来获取和打印用户输入。

#include <stdio.h>

static char input[2048];

int main(int argc, char** argv) {
        puts("Brenlisp Version 0.0.0.0.1");
        puts("Press Ctrl+c to Exit\n");

        while (1) {
                fputs("brenlisp> ", stdout);
                fgets(input, 2048, stdin);
                fputs(input, stdout);
        }

        return 0;
}

When I ran the executable from a Bash terminal (Windows 10), the program started but did not print anything to console, nor did it accept/print user input.当我从 Bash 终端(Windows 10)运行可执行文件时,程序启动但没有向控制台打印任何内容,也没有接受/打印用户输入。

bschw@DESKTOP-92VUB1F MINGW64 ~/Projects/brenlisp
$ ./prompt.exe

However, when I ran the executable from CMD, the prompt performed as expected:但是,当我从 CMD 运行可执行文件时,提示按预期执行:

C:\Users\bschw\Projects\brenlisp>prompt.exe
BrenLisp Version 0.0.0.0.1
Press Ctrl+c to Exit

brenlisp> works fine
works fine
brenlisp> works fine
brenlisp> ^C
C:\Users\bschw\Projects\brenlisp>

Another curious thing is that when I run the program on WSL, it doesn't print the "^C" string to the console before exiting:另一个奇怪的事情是,当我在 WSL 上运行程序时,它在退出之前不会将“^C”字符串打印到控制台:

bschw@DESKTOP-92VUB1F:/mnt/c/Users/bschw/Projects/brenlisp$ ./prompt.exe
BrenLisp Version 0.0.0.0.1
Press Ctrl+c to Exit

brenlisp> works
works
brenlisp> works
brenlisp> bschw@DESKTOP-92VUB1F:/mnt/c/Users/bschw/Projects/brenlisp$

Why are these programs behaving differently depending on which shell they're being run from?为什么这些程序的行为取决于它们从哪个 shell 运行? How could I get the program to work properly on Bash?我怎样才能让程序在 Bash 上正常工作?

In many implementations, the C Standard Library buffers output in internal buffers (*).在许多实现中,C 标准库在内部缓冲区 (*) 中缓冲输出。

Often, for text streams, it does "line buffering" (buffers get emptied after dealing with a '\\n' ).通常,对于文本流,它执行“行缓冲”(处理'\\n'后缓冲区被清空)。 This appears to be your case.这似乎是你的情况。 Your output des not contain a newline.您的输出 des 不包含换行符。

To force the buffers to be emptied, use fflush() for output operations.要强制清空缓冲区,请使用fflush()进行输出操作。

printf("not a line");
fflush(stdout);             // force buffer emptying

printf("complete line\n");  // line-buffering poses no issue here

(*) input streams may also be buffered, but the management of buffering for input is quite different (*) 输入流也可以被缓冲,但是输入缓冲的管理是完全不同的

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

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