what does it mean 'fork()' will copy address space of original process

10,608

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.

Share:
10,608

Related videos on Youtube

kwagjj
Author by

kwagjj

Updated on September 18, 2022

Comments

  • kwagjj
    kwagjj over 1 year

    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:

    pid=fork();
    if(pid<0){ //error stuff
    }
    else if(pid==0){
    // child process stuff
    }
    else{
    // 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?

  • kwagjj
    kwagjj almost 9 years
    thanks! I haven't reached to the threads chapter but thanks for the heads up! really interesting stuff
  • iCantC
    iCantC over 3 years
    Since the parent address space is copied for the child process, both the address spaces are identical, this implies if a random variable int a = 10 in parent address space has a memory address say 1000 then the variable int a = 10 even in child address space will also be having memory address 1000.