📜  fork()和使用它创建的内存共享黑白进程

📅  最后修改于: 2021-05-26 03:23:03             🧑  作者: Mango

先决条件:C语言中的fork()。

因此,当我们执行fork()时,两个进程实际上共享哪些部分?
是每个进程的堆内存吗?
是否共享全局变量?
malloc会向两者返回相同的地址吗?

让我们在下面的程序中运行并查看它的输出以清除上面的问题。

// C program to demonstrate working of fork()
#include 
#include 
#include 
#include 
#include 
#include 
  
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("\n Child Process Initial Value :: localVar"
                   " = %d, globalVar = %d", localVar,
                   globalVar);
            localVar++;
            globalVar++;
  
            int c = 500;
            printf("\n Child Process :: localVar = %d, "
                   "globalVar = %d", localVar, globalVar);
            printf("\n Address of malloced mem child = %p "
                   "and value is %d", p, *p);
            printf("\n lets change the value pointed my malloc");
  
            *p = 50;
            printf("\n Address of malloced mem child = %p "
                   "and value is %d", p, *p);
            printf("\n lets change the value pointed my "
                   "malloc in child");
  
            *p = 200;
            printf("\n Address of malloced mem child = %p "
                   "and value is %d\n\n\n", p, *p);
        }
        else // Parent process
        {
            printf("\n Parent process Initial Value :: "
                   "localVar = %d, globalVar = %d",
                   localVar, globalVar);
  
            localVar = 10;
            globalVar = 20;
            printf("\n Parent process :: localVar = %d,"
                  " globalVar = %d", localVar, globalVar);
            printf("\n Address of malloced mem parent= %p "
                   "and value is %d", p, *p);
  
            *p = 100;
            printf("\n Address of malloced mem parent= %p "
                   "and value is %d", p, *p);
            printf("\n lets change the value pointed my"
                    " malloc in child");
            *p = 400;
            printf("\n Address of malloced mem child = %p"
                   " and value is %d \n", p, *p);
        }
    }
    else // fork failed
    {
        printf("\n Fork failed, quitting!!!!!!\n");
        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。
    想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。