0) { for(i=0;i" />
红联Linux门户
Linux帮助

父进程和子进程的地址空间问题?

发布时间:2011-08-31 11:06:39来源:红联作者:beamjiang
#include"stdio.h"
#include "unistd.h"

int main()
{

pid_t pid;
int var =0;
int i;
printf("for test!\n");
printf("in parent process!\n");
pid = fork();
if(pid > 0)
{
for(i=0;i<3;i++)

printf("after call fork,im parent process,var[%d],&var[%d]\n",var++,(&var));
}
else if(pid == 0)
{
for(i=0;i<5;i++)

printf("after call fork,im child process,var[%d],&var[%d]\n",var++,&var);

}
else
printf("after call fork,call fork failed !\n");
return 0;
}

结果输出:
for test!
in parent process!
after call fork,im parent process,var[0],&var[2280776]after call fork,im child p
rocess,var[0],&var[2280776]
after call fork,im parent process,var[1],&var[2280776]after call fork,im child p
rocess,var[1],&var[2280776]
after call fork,im parent process,var[2],&var[2280776]after call fork,im child p
rocess,var[2],&var[2280776]
after call fork,im child process,var[3],&var[2280776]
after call fork,im child process,var[4],&var[2280776]

为什么变量var的地址一样,数值却不能进行累加呢?“多进程地址空间是独立的,虽然两个指针的值相同,但他们是不同进程空间的”,谁能给解释下不同进程地址空间么?
文章评论

共有 0 条评论