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 .
C
// 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> #include <sys/mman.h> int globalVar; /* A global variable*/ int main( void ) { int localVar = 0; int * p = ( int *) mmap(NULL, sizeof ( int ) , PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); pid_t childPID = fork(); // Putting value at allocated address *p = 0; if (childPID >= 0) // fork was successful { if (childPID == 0) // child process { // sleep (1); 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); exit (EXIT_SUCCESS); } else { wait(NULL); // sleep(1); 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.
- A way to allocate memory to be shared between processes is using function mmap. Beside that, to guarantee that parent process access changed value made by child process, the program need a synchronization point. this can be made using function wait(NULL). With this, parent process wait chil process has finished to go ahead.
Please Login to comment...