![]() |
VOOZH | about |
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 the program below and take a look at the output of it to clear the questions above.
Output
Child Process Initial Value :: localVar = 0, globalVar = 0 Address of malloced privateMem in child = 0x7f245277d000 and value is 0 Address of malloced sharedMem in child = 0x7f2452750000 and value is 0 Updated child Process :: localVar = 1, globalVar = 2 lets change the value of privateMem variable created by malloc in child Address of malloced privateMem in child = 0x7f245277d000 and value is 50 lets change the value of sharedMem variable created my malloc in child Address of malloced sharedMem in child = 0x7f2452750000 and value is 100 Parent Process Initial Value :: localVar = 0, globalVar = 0 Address of malloced privateMem in parent = 0x7f245277d000 and value is 0 Address of malloced sharedMem in parent = 0x7f2452750000 and value is 100 Updated parent process :: localVar = 10, globalVar = 20 lets change the value of privateMem variable created by malloc in parent Address of malloced privateMem in parent= 0x7f245277d000 and value is 100 lets change the value of sharedMem variable created my malloc in parent Address of malloced sharedMem in parent = 0x7f2452750000 and value is 400
Explanation