[理工] 作業系統的process forking原理
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
11/16 21:29, 1F
推
11/16 21:38, , 2F
11/16 21:38, 2F
推
11/16 22:01, , 3F
11/16 22:01, 3F
推
11/16 22:03, , 4F
11/16 22:03, 4F
目前是沒有正確解答啦 我還在找
只不過因為我程式很差 所以比較想知道是什麼樣的過程來處理出這些數字...
→
11/16 22:15, , 5F
11/16 22:15, 5F
→
11/16 22:15, , 6F
11/16 22:15, 6F
→
11/16 22:15, , 7F
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
11/16 23:54, 9F
→
11/16 23:54, , 10F
11/16 23:54, 10F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):