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.
- fork() to execute processes from bottom to up using wait()
- Concurrent Merge Sort in Shared Memory
- IPC through shared memory
- Inter-process Communication using a shared stack
- Difference between Static and Shared libraries
- Working with Shared Libraries | Set 1
- Working with Shared Libraries | Set 2
- Maximum number of threads that can be created within a process in C
- Zombie and Orphan Processes in C
- Zombie Processes and their Prevention
- Calculation in parent and child process using fork()
- Factorial calculation using fork() in C for Linux
- Creating multiple process using fork()
- Create n-child process from same parent process using fork() in C
- Creating child process using fork() in Python
- fork() and Binary Tree
- C program to demonstrate fork() and pipe()
- Difference between fork() and exec()
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- fork() in C
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.