[理工] [作業系統]課本fork問題

看板Grad-ProbAsk作者 (米卡莎美琴)時間3年前 (2022/10/23 11:49), 編輯推噓2(200)
留言2則, 2人參與, 3年前最新討論串1/1
恐龍本題目 3.1 Using the program shown in Figure 3.30, explain what the output will be at Line A. Figure 3.30: #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 5; int main() { pid t pid; pid = fork(); if (pid == 0) { /* child process */ value += 15; return 0; } else if (pid > 0) { /* parent process */ wait(NULL); printf("PARENT: value = %d",value); /* LINE A * return 0; } } 解答: Answer: The result is still 5 as the child updates its copy of value. When control returns to the parent, its value remains at 5. 奇怪有wait的時候父不是會等子做完嗎? 這樣value應該有更新成20了吧 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.77.167.64 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Grad-ProbAsk/M.1666496987.A.2C2.html

10/23 13:03, 3年前 , 1F
如同解答寫的 子程序的變數會是副本 跟父程序不相干
10/23 13:03, 1F

10/23 15:57, 3年前 , 2F
data section 是分開的
10/23 15:57, 2F
文章代碼(AID): #1ZLBdRB2 (Grad-ProbAsk)