- 论坛徽章:
- 0
|
问题:变量在子进程和父进程中看来用了同样的地址空间,但是为什么在子进程中把该地址空间的值改变了,却对父进程中变量的值没有影响?
刚刚开始学习,非常疑惑,还望各位老大不吝赐教,谢谢!
代码如下:
#include <stdio.h>
#include <sys/types.h>
int main ()
{
pid_t pid;
static int iStatic = 1;
int iAuto = 1;
int * iPtr;
iPtr = &iStatic;
switch(pid = fork())
{
case -1:
return 1;
case 0:
iStatic += 1;
iAuto += 1;
*iPtr += 1;
printf("%d\t%d\t%d\n",iStatic,iAuto,*iPtr);
printf("%p\t%p\t%p\n",&iStatic,&iAuto,iPtr);
break;
default:
wait(NULL);
printf("%d\t%d\t%d\n",iStatic,iAuto,*iPtr);
printf("%p\t%p\t%p\n",&iStatic,&iAuto,iPtr);
}
return 0;
}
输出结果如下:
3 2 3
0x80496bc 0xbff9f490 0x80496bc
1 1 1
0x80496bc 0xbff9f490 0x80496bc |
|