4
ทำไม cat x >> x loop
คำสั่ง bash ต่อไปนี้จะเข้าสู่วง infinte: $ echo hi > x $ cat x >> x ฉันเดาได้ว่าcatอ่านต่อไปxหลังจากเริ่มเขียนไปยัง stdout แล้ว อย่างไรก็ตามสิ่งที่น่าสับสนคือการทดสอบการใช้งานแมวของฉันแสดงพฤติกรรมที่แตกต่าง: // 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); …