#include <stdio.h>
#include <unistd.h>
int main()
{
int a = 1, b = 1;
int rval, pid;
pid = fork();
switch(pid)
{
case -1:
printf("I am bad.\n");
case 0:
printf("I am in child.\n");
rval = a + b;
printf("leaving child.\n");
default:
wait();
printf("I am back to parent.\n");
//wait();
printf("%d \n", rval);
printf("leaving parent.\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我首先期待孩子的陈述,然后是父母.但是不应该有任何好处.代替,
>> ./demo
I am in child.
leaving child.
I am back to parent.
2
leaving parent.
I am back to parent.
134513584
leaving parent.
Run Code Online (Sandbox Code Playgroud)
父母重复两次.这是为什么?此外,父母是如何2从孩子那里得到的?Child有自己的虚拟内存a,b所以我们如何将结果传递给父母?我很迷惑.
进一步的调查.当我插入return 0到case 0,也不会重复.看起来switch语句仍在继续.我错过了关于switch语句的一些内容吗?
| 归档时间: |
|
| 查看次数: |
704 次 |
| 最近记录: |