恐龍本題目
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了吧
感謝