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?