In many programs and man pages of Linux, I have seen code using fork()
. Why do we need to use fork()
and what is its purpose?
fork()
is used to spawn a child process. Typically it's used in similar sorts of situations as threading, but there are differences. Unlike threads, fork()
creates whole seperate processes, which means that the child and the parent while they are direct copies of each other at the point that fork()
is called, they are completely seperate, neither can access the other's memory space (without going to the normal troubles you go to access another program's memory).
fork()
is still used by some server applications, mostly ones that run as root on a *NIX machine that drop permissions before processing user requests. There are some other usecases still, but mostly people have moved to multithreading now.