fork() and memory shared b/w processes created using it - GeeksforGeeks

28 Jun, 2021
Prerequisite : fork() in C.
So when we do a fork() what are the sections the two process actually share ?Is the heap memory per process ?Are the global variables shared ?Will malloc return the same address to both ?
Let us run below program and take look at the output of it to clear the questions above .
// 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("\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

Explanation ::
So each process child and parent both have their own copy of globalVariable and localvar otherwise if they had shared it, we would have got “Child Process Initial Value :: localVar = [10], globalVariable[20] ” in child process which was assigned in parent process which executed first but we didn’t.Though address of memory returned by malloc is same but in actual they are pointing to or mapped to different physical address otherwise when parent assigned value of 100 at memory address 0x1535010 the same 100 we should have got in child but we got 0.This article is contributed by Abhilash Kumar Jaiswal . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
So each process child and parent both have their own copy of globalVariable and localvar otherwise if they had shared it, we would have got “Child Process Initial Value :: localVar = [10], globalVariable[20] ” in child process which was assigned in parent process which executed first but we didn’t.
Though address of memory returned by malloc is same but in actual they are pointing to or mapped to different physical address otherwise when parent assigned value of 100 at memory address 0x1535010 the same 100 we should have got in child but we got 0.This article is contributed by Abhilash Kumar Jaiswal . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
This article is contributed by Abhilash Kumar Jaiswal . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
system-programming
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
'this' pointer in C++
How to split a string in C/C++, Python and Java?
Smart Pointers in C++ and How to Use Them
How to dynamically allocate a 2D array in C?
UDP Server-Client implementation in C
Input-output system calls in C | Create, Open, Close, Read, Write