views:

328

answers:

3

hi everyone,

my question is somewhat conceptual, how is parent process' data shared with child process created by a fork() call or with a thread created by pthread_create()

for example, are global variables directly passed into child process and if so, does modification on that variable made by child process effect value of it in parent process?

i appreciate partial and complete answers in advance, if i'm missing any existing resource, i'm sorry, i've done some search on google but couldn't find good results

thanks again for your time and answers

+2  A: 

A fork creates an almost exact copy of the calling process, including memory and file descriptors. Global variables are copied along with everything else, but they are not in any way linked to the parent process. Since file descriptors are also copied, parent and child can interact via these (as long as they're setup properly, usually via pipe or socketpair).

Marcelo Cantos
how about this case, i have an int* in parent process, when forked, it points somewhere like address 5, the child copy will point to the same place right? and modification will take effect also in parent process
mustafabattal
No. The entire memory image is copied. It doesn't matter whether it's a variable or some undisclosed piece of memory.
Marcelo Cantos
+1  A: 

There's a big difference between processes created by fork and between threads created with pthread_create. Processes don't share global variables and should communicate through pipes, sockets, or other tools provided by the OS. A good solution is MPI - which is a message-passing library for inter-process communication.

Threads are quite different. A thread created with pthread_create shares all the global variables with its caller. Moreover, the caller can pass an arbitrary structure into the thread, and this structure will also be shared. This means that one should be extremely careful when programming with threads - such amounts of sharing are dangerous and error prone. The pthread API provides mutexes and conditions for robust synchronization between threads (although it still requires practice and expertise to implement correctly).

Eli Bendersky
+2  A: 

The semantics of fork() and pthread_create() are a little different.

fork() will create a new process, where the global variables will be separate between the parent and children. Most OS implementations will use copy-on-write semantics, meaning that both the parent and child process will use the same physical memory pages for all global variables until one of the processes attempts to edit the physical memory, at which point a copy of that page is made, so that now each process gets its own copy and does not see the other process's, so that the processes are isolated.

pthread_create() on the other hand, creates a new thread within the same process. The new thread will have a separate stack space from the other running threads of the same process, however the global variables and heap space are shared between all threads of the same process. This is why you often need a mutex to coordinate access to a shared piece of memory between multiple threads of the same process.

TL;DR version: with fork(), you don't see the other guy's changes; with pthread_create() you do.

RarrRarrRarr