Upon reading “Linux Kernel Development” I saw next statetement:
Traditionally, upon
fork(), all resources owned by the parent are duplicated and the
copy is given to the child.This approach is naive and inefficient in that it copies much
data that might otherwise be shared.Worse still, if the new process were to immediately
execute a new image, all that copying would go to waste
Why do fork() need to copy all parent’s resources? Why we can’t simply allot freshly new chunk of space for all resources needed by new process? Why is there neediness in copying? And the last query – if the new process were to immediately execute the new image, why would that copying go to waste?
The semantics of
forks(2)say that when you do it another process starts executing from that point. So if it starts executing, it will naturally have some expectations regarding declared variables, their values and so on. You need to copy everything* the parent had access to.This copying is completely useless if the new process turns out not to need continue from that point. For example, if the new process simply wants to start executing a new program, it won’t need any of those variables mentioned above.