fork()和使用它创建的内存共享b/w进程

先决条件: C中的fork() .

null

那么,当我们做fork()时,这两个进程实际上共享哪些部分呢? 每个进程的堆内存是多少? 全局变量是否共享? malloc会将相同的地址返回给双方吗?

让我们运行下面的程序,看看它的输出,以澄清上面的问题。

// C program to demonstrate working of fork()
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int globalVar; /*  A global variable*/
int main( void )
{
int localVar = 0;
int * p = ( int *) malloc (2);
pid_t childPID = fork();
// Putting value at allocated address
*p = 0;
if (childPID >= 0) // fork was successful
{
if (childPID == 0) // child process
{
printf ( " Child Process Initial Value :: localVar"
" = %d, globalVar = %d" , localVar,
globalVar);
localVar++;
globalVar++;
int c = 500;
printf ( " Child Process :: localVar = %d, "
"globalVar = %d" , localVar, globalVar);
printf ( " Address of malloced mem child = %p "
"and value is %d" , p, *p);
printf ( " lets change the value pointed my malloc" );
*p = 50;
printf ( " Address of malloced mem child = %p "
"and value is %d" , p, *p);
printf ( " lets change the value pointed my "
"malloc in child" );
*p = 200;
printf ( " Address of malloced mem child = %p "
"and value is %d" , p, *p);
}
else // Parent process
{
printf ( " Parent process Initial Value :: "
"localVar = %d, globalVar = %d" ,
localVar, globalVar);
localVar = 10;
globalVar = 20;
printf ( " Parent process :: localVar = %d,"
" globalVar = %d" , localVar, globalVar);
printf ( " Address of malloced mem parent= %p "
"and value is %d" , p, *p);
*p = 100;
printf ( " Address of malloced mem parent= %p "
"and value is %d" , p, *p);
printf ( " lets change the value pointed my"
" malloc in child" );
*p = 400;
printf ( " Address of malloced mem child = %p"
" and value is %d " , p, *p);
}
}
else // fork failed
{
printf ( " Fork failed, quitting!!!!!!" );
return 1;
}
return 0;
}


 Parent process Initial Value :: localVar = 0, globalVar = 0
 Parent process :: localVar = 10, globalVar = 20
 Address of malloced mem parent= 0x1bb5010 and value is 0
 Address of malloced mem parent= 0x1bb5010 and value is 100
 lets change the value pointed my malloc in child
 Address of malloced mem child = 0x1bb5010 and value is 400 

 Child Process Initial Value :: localVar = 0, globalVar = 0
 Child Process :: localVar = 1, globalVar = 1
 Address of malloced mem child = 0x1bb5010 and value is 0
 lets change the value pointed my malloc
 Address of malloced mem child = 0x1bb5010 and value is 50
 lets change the value pointed my malloc in child
 Address of malloced mem child = 0x1bb5010 and value is 200

说明::

  1. 因此,每个进程的子进程和父进程都有自己的globalVariable和localvar副本,否则,如果他们共享了它,我们会在子进程中得到“子进程初始值::localvar=[10],globalVariable[20]”,这是在首先执行的父进程中分配的,但我们没有。
  2. 虽然malloc返回的内存地址相同,但实际上它们指向或映射到不同的物理地址,否则当父级在内存地址0x1535010处分配值100时,我们应该在子级中得到相同的100,但我们得到了0。

    本文由 阿比拉什·库马尔·贾斯瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享