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 .
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
- 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 = , globalVariable ” 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.
- IPC through shared memory
- Concurrent Merge Sort in Shared Memory
- fork() to execute processes from bottom to up using wait()
- Working with Shared Libraries | Set 2
- Working with Shared Libraries | Set 1
- Difference between Static and Shared libraries
- Inter-process Communication using a shared stack
- Maximum number of threads that can be created within a process in C
- Zombie Processes and their Prevention
- Zombie and Orphan Processes in C
- fork() in C
- sorting in fork()
- searching in fork()
- Fork() Bomb
- C vs BASH Fork bomb
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 email@example.com. 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.