# linux 2021: sj82516 ## γ 根據 [linux man page fork(2)](https://man7.org/linux/man-pages/man2/fork.2.html),fork 呼叫時會從執行的 process 複製出一份 child process,child process 會複製相同的記憶體內容但獨立的空間(copy-on-write),所以後續 child process 與 parent process 做出的記憶體修改是不互相影響的,其中複製的內容就包括 for 迴圈的變數 i >The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables ... 所以當 NNN = 1 時,會輸出 2 個 "-",因為 parent process 執行第一次會 fork 一份 child process,兩個 process 個印出一次 "-" 共為 2; 當 NNN = 2 時,i = 0 時 fork 出一個 child process,i = 1 時,parent / child process 在各自 fork 出 child process,此時 child process 中的 i 為 1,最終打印次數為 2 + (2 * 2 + 2) = 8 ``` i = 0 => p c i = 1 => p/\c c/\c' ``` 當 NNN = 3,打印次數為 2 * 2^ + 2 * 2^1 + 1 * 2^2 = 3 * 2^3 = 24 次 推斷出公式為 NNN * (2 ^ NNN),得出要打印 49152 次則 NNN 為 12 ## ζ
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up