'Forking vs Threading
I have used threading before in my applications and know its concepts well, but recently in my operating system lecture I came across fork(). Which is something similar to threading.
I google searched difference between them and I came to know that:
- Fork is nothing but a new process that looks exactly like the old or the parent process but still it is a different process with different process ID and having it’s own memory.
- Threads are light-weight process which have less overhead
But, there are still some questions in my mind.
- When should you prefer fork() over threading and vice-verse?
- If I want to call an external application as a child, then should I use fork() or threads to do it?
- While doing google search I found people saying it is bad thing to call a fork() inside a thread. why do people want to call a fork() inside a thread when they do similar things?
- Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?
Solution 1:[1]
A forked process is called a heavy-weight process, whereas a threaded process is called light-weight process.
The following are the difference between them:
- A forked process is considered a child process whereas a threaded process is called a sibling.
- Forked process shares no resource like code, data, stack etc with the parent process whereas a threaded process can share code but has its own stack.
- Process switching requires the help of OS but thread switching it is not required
- Creating multiple processes is a resource intensive task whereas creating multiple thread is less resource intensive task
- Each process can run independently whereas one thread can read/write another threads data.
Thread and process lecture

Solution 2:[2]
fork() spawns a new copy of the process, as you've noted. What isn't mentioned above is the exec() call which often follows. This replaces the existing process with a new process (a new executable) and as such, fork()/exec() is the standard means of spawning a new process from an old one.
e.g. that's how your shell will invoke a process from the command line. You specify your process (ls, say) and the shell forks and then execs ls.
Note that this operates at a very different level from threading. Threading runs multiple lines of execution intra-process. Forking is a means of creating new processes.
Solution 3:[3]
As @2431234123412341234123 said, on Linux thanks to COW, processes are not much heavier than threads and boils down to their usage. COW - copy on write means that a memory page of the forked process gets copied only when forked process makes changes to it, otherwise OS keeps redirecting it to pages of the parent process.
From a programming use case, let us say in the heap memory you have a big data structure a 2d array[2000000][100] (200 mb), and the page size of the kernel is around 4 mb. When the process is forked, no new memory for this array will be allocated. If one particular row (100 bytes) is changed (in either parent process or child), only the corresponding page (4 kb or 8kb if it is overlapping in two pages) will be copied and updated for the forked thread.
Other memory portions of memory work in forked processes same as threads (code is same, registers and call stack are separate).
On Windows as @Niels Keurentjes said, thrads might be better from a performance view, but on Linux it is more of use case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sam R. |
| Solution 2 | Brian Agnew |
| Solution 3 | Varun Garg |
