Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing heap memory with fork()
    primarykey
    data
    text
    <p>I am working on implementing a database server in C that will handle requests from multiple clients. In order to do so I am using fork() to handle connections for individual clients. </p> <p>The server stores data in the heap which consists of a root pointer to hash tables of dynamically allocated records. The records are structs that have pointers to various data-types. I would like for the processes to be able to share this data so that when a client makes a change to the heap the changes will be visible for the other clients.</p> <p>I have learned that fork() uses <a href="http://en.wikipedia.org/wiki/Copy-on-write">COW (Copy On Write)</a> and my understanding is that it will copy the heap (and stack) memory of the parent process when the child will try to modify the data in memory. </p> <p>I have found out that I can use the shm library to share memory. </p> <p>-Would it suffice to share the root pointer of the database or do I have to make all allocated memory as shared? </p> <p>-If a child allocates memory will the parent / other children be able to access it? </p> <p>-Also if a child allocates memory and is later killed will the allocated memory still stay on the heap? </p> <p>So for example would the code below be a valid way to share heap memory (in shared_string)? If a child were to use similar code (i.e. starting from //start ) would other children be able to read/write to it while the child is running and after it's dead?</p> <pre><code>key_t key; int shmid; key = ftok("/tmp",'R'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); //start char * string; string = malloc(sizeof(char) * 10); strcpy(string, "a string"); char * shared_string; shared_string = shmat(shmid, string, 0); strcpy(shared_string, string); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload