I'm studying 'operation system concepts' on my own and I'm studying the chp3. process part.

There is an example where the 'fork()' function is called and depending of the returned pid value like the following:

if(pid<0){ //error stuff
else if(pid==0){
// child process stuff
// parent process stuff

What confused me here is that if this code is executed, among the three scenarios of 'if's, only one would be executed which means that only one out of parent/child procedures would be executed.

But reading a bit more carefully, I found a sentence that kind of helped me solve the confusion but not entirely.

The new process consists of a copy of the address space of the original process.

From my imagination, I guess this means that whenever the 'fork()' calls is executed, somehow an exact copy of this code will be duplicated and be run as 'child' process when the original c code will be run as 'parent'.

Am I understanding this right?

Also, what does the 'address space' have to do with this? Again, using my imagination I assume that, since the execution of parent code means that the code is loaded to the RAM and executed where it will have a segment of RAM assigned to the code, this segment will be copied to a new segment somewhere else located in the RAM and be assigned for the child process.

Is my understanding correct?

up vote 6 down vote accepted

Yes, you are correct.

In particular, this means that the child will inherit all variables from the parent process with the value they had at the moment of the fork. However, if at a later step one of the parent or the child modifies one of these variables, the modification will be local to this process: if the child modify a variable, the parent process will still see the old value and not the new one.

With forks, if you want the child and parent process to communicate you will need to use some explicit inter-process communication.

This is the difference with threads. Conceptually forks and threads look the same: the same code being executed by two processes in the case of forks and two threads in the case of threads. However, in the case of threads the address space will not be copied: the two threads will share the same memory, so if one thread modify a variable, it will impact all other threads.

Threads therefore allows a very flexible communication between the threads, but this is also very error prone because of the high probability of race conditions if not used carefully.

Both systems are address different needs. As a side note, the fork primitive is usually implemented in a clever way on the system side since the address space will not be physically copied, but the system will use a copy-on-write system: data will be duplicated only if one of the processes attempts to actually modify it. While the data is not modified it will not be duplicated and will therefore not consume more memory.

More information regarding forks and thread can be found on StackOverflow.

  • thanks! I haven't reached to the threads chapter but thanks for the heads up! really interesting stuff – kwagjj Jun 6 '15 at 9:22

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.