[理工] 作業系統的process forking原理

看板Grad-ProbAsk作者 (i服了u)時間9年前 (2014/11/16 20:54), 9年前編輯推噓5(505)
留言10則, 3人參與, 最新討論串1/2 (看更多)
Suppose that the process id of parent process and child process are 1999 and 2013 respectively. List the output generated at Line X, Y and Z. 父,子行程的process id 分別為1999 以及2013。請寫出Line X, Y and Z 的輸出。 #include <sys/types.h> #include <stdio.h> #include <unistd.h> #define SIZE 5 int nums[SIZE] = {0, 1, 2, 3, 4}; int main() { int i; pid_t pid; pid = fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] *= -i; printf(" %d ", nums[i]); /* LINE X */ } }else if (pid > 0) { printf("My Process ID is %d \n", getpid()); /* LINE Y */ wait(NULL); for (i = 0; i < SIZE; i++) printf(" %d ", nums[i]); /* LINE Z */ } return 0; } 上面這個習題找不到講義或有用的網頁來了解原理 請問有人知道創造pid的forking該怎麼一步步解釋? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.57.133.45 ※ 文章網址: http://www.ptt.cc/bbs/Grad-ProbAsk/M.1416142499.A.E7C.html

11/16 21:29, , 1F
pid=0 是孩子,然後孩子改了不會影響父親的。
11/16 21:29, 1F

11/16 21:38, , 2F
不是要看題目有沒有假設變數是否共享嗎
11/16 21:38, 2F

11/16 22:01, , 3F
我想知道X和Z的答案 是各有五行嗎?
11/16 22:01, 3F

11/16 22:03, , 4F
0 -1 -2 -3 -4。1999。0 -1 -2 -3 -4?
11/16 22:03, 4F
目前是沒有正確解答啦 我還在找 只不過因為我程式很差 所以比較想知道是什麼樣的過程來處理出這些數字...

11/16 22:15, , 5F
做完fork都變成兩個process了,你有看過一個process改自
11/16 22:15, 5F

11/16 22:15, , 6F
己的東西可以順便改其他人的?就好像開兩個IE,雖然都是
11/16 22:15, 6F

11/16 22:15, , 7F
IE,可是一個看YAHOO不影響一個看GOOGLE阿。
11/16 22:15, 7F
先謝謝你囉~! ※ 編輯: ifooleru (61.57.133.45), 11/16/2014 23:54:51

11/16 23:54, , 8F
洪逸的筆記有寫到有些題目可能會假設變數為共享 當然
11/16 23:54, 8F

11/16 23:54, , 9F
實際上真的要share就要另外處理了 不過我翻了他書的後
11/16 23:54, 9F

11/16 23:54, , 10F
面考古題好像也沒有出現過 所以...就當我沒說過吧:d
11/16 23:54, 10F
文章代碼(AID): #1KQ9wZvy (Grad-ProbAsk)
文章代碼(AID): #1KQ9wZvy (Grad-ProbAsk)