# linux2021: hankluo6 ## γ - 1 * 解釋上述程式碼輸出 `-` 字元數量的原理 每次呼叫 `fork` 時,stdout 內部的 buffer data 也會跟著複製,所以 parent process 與 child process 內部各有一份自己保管的 buffer data,在 `printf` 的時候,因為沒有將 buffer 清空,導致 stdout 會以預設超過 1024 字元或是 flush 操作時才會印出 buffer 內容。 每一次迴圈內的 `fork` 都會讓當前 process 數加倍,假設迴圈為 $x$ 次,則 process 數總共為 $2^x$,而每次 `fork` 出來的 process 內部 buffer data 都繼承其 parent process 的 buffer data,再加上剩餘迴圈次數中的 `printf('-')`,可得每個 process 的 buffer 內的 `-` 數量為 $x$,即可求出 $x = 12$: * $2^x \times x = 49152,\ x = 12$