Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifically, how does fork() handle dynamically allocated memory from malloc() in Linux?

I have a program with a parent and a child process. Before the fork(), the parent process called malloc() and filled in an array with some data. After the fork(), the child needs that data. I know that I could use a pipe, but the following code appears to work:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>  int main( int argc, char *argv[] ) {     char *array;     array = malloc( 20 );     strcpy( array, "Hello" );     switch( fork() ) {     case 0:         printf( "Child array: %s\n", array );         strcpy( array, "Goodbye" );         printf( "Child array: %s\n", array );         free( array );         break;     case -1:         printf( "Error with fork()\n" );         break;     default:         printf( "Parent array: %s\n", array );         sleep(1);         printf( "Parent array: %s\n", array );         free( array );     }     return 0; } 

The output is:

Parent array: Hello Child array: Hello Child array: Goodbye Parent array: Hello 

I know that data allocated on the stack is available in the child, but it appears that data allocated on the heap is also available to the child. And similarly, the child cannot modify the parent's data on the stack, the child cannot modify the parent's data on the heap. So I assume the child gets its own copy of both stack and heap data.

Is this always the case in Linux? If so, where the is the documentation that supports this? I checked the fork() man page, but it didn't specifically mention dynamically allocated memory on the heap.

like image 236
pcd6623 Avatar asked Jan 04 '11 20:01

pcd6623


People also ask

When memory is dynamically allocated with malloc () Where is it allocated?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

What is the correct way to allocate memory dynamically using malloc () function?

Syntax of malloc()ptr = (float*) malloc(100 * sizeof(float)); The above statement allocates 400 bytes of memory. It's because the size of float is 4 bytes. And, the pointer ptr holds the address of the first byte in the allocated memory.

Does fork allocate memory?

Thus, the fork system call allocates new memory for those segments in the child (the same size as the segments in the parent) and copies the contents of each segment from the parent to the corresponding segment in the child.

Does malloc allocate memory dynamically?

C malloc() methodThe “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.


1 Answers

Each page that is allocated for the process (be it a virtual memory page that has the stack on it or the heap) is copied for the forked process to be able to access it.

Actually, it is not copied right at the start, it is set to Copy-on-Write, meaning once one of the processes (parent or child) try to modify a page it is copied so that they will not harm one-another, and still have all the data from the point of fork() accessible to them.

For example, the code pages, those the actual executable was mapped to in memory, are usually read-only and thus are reused among all the forked processes - they will not be copied again, since no one writes there, only read, and so copy-on-write will never be needed.

More information is available here and here.

like image 83
abyx Avatar answered Sep 28 '22 08:09

abyx