4
为什么猫x >> x循环?
以下bash命令进入无限循环: $ echo hi > x $ cat x >> x 我可以猜到它开始写到stdout之后会cat继续读取x。但是,令人困惑的是,我自己的cat测试实现表现出不同的行为: // mycat.c #include <stdio.h> int main(int argc, char **argv) { FILE *f = fopen(argv[1], "rb"); char buf[4096]; int num_read; while ((num_read = fread(buf, 1, 4096, f))) { fwrite(buf, 1, num_read, stdout); fflush(stdout); } return 0; } 如果我运行: $ …