[理工] 作业系统的process forking原理

楼主: ifooleru (i服了u)   2014-11-16 20:54:56
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该怎么一步步解释?
谢谢
作者: A4P8T6X9 (残废的名侦探)   2014-11-16 21:29:00
pid=0 是孩子,然后孩子改了不会影响父亲的。
作者: qoozxc789 (呵呵)   2014-11-16 21:38:00
不是要看题目有没有假设变量是否共享吗
作者: j897495 (咪咪)   2014-11-16 22:01:00
我想知道X和Z的答案 是各有五行吗?0 -1 -2 -3 -4。1999。0 -1 -2 -3 -4?
作者: A4P8T6X9 (残废的名侦探)   2014-11-16 22:15:00
做完fork都变成两个process了,你有看过一个process改自己的东西可以顺便改其他人的?就好像开两个IE,虽然都是IE,可是一个看YAHOO不影响一个看GOOGLE阿。
作者: qoozxc789 (呵呵)   2014-11-16 23:54:00
洪逸的笔记有写到有些题目可能会假设变量为共享 当然实际上真的要share就要另外处理了 不过我翻了他书的后面考古题好像也没有出现过 所以...就当我没说过吧:d

Links booklink

Contact Us: admin [ a t ] ucptt.com