作者mikasamikoto (米卡莎美琴)
看板Grad-ProbAsk
标题[理工] [作业系统]课本fork问题
时间Sun Oct 23 11:49:41 2022
恐龙本题目
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://webptt.com/cn.aspx?n=bbs/Grad-ProbAsk/M.1666496987.A.2C2.html
1F:推 KaryuuIssen: 如同解答写的 子程序的变数会是副本 跟父程序不相干 10/23 13:03
2F:推 jemmy9211: data section 是分开的 10/23 15:57