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

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 .



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


 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 ::

  1. 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.
  2. 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.
  3. This article is contributed by Abhilash Kumar Jaiswal . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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_up


    Article Tags :
    Practice Tags :


    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.