Difference between pthread and fork on gnu/Linux
Solution 1
In C there are some differences however:
fork()
Purpose is to create a new process, which becomes the child process of the caller
Both processes will execute the next instruction following the fork() system call
Two identical copies of the computer's address space,code, and stack are created one for parent and child.
Thinking of the fork as it was a person; Forking causes a clone of your program (process), that is running the code it copied.
pthread_create()
Purpose is to create a new thread in the program which is given the same process of the caller
Threads within the same process can communicate using shared memory. (Be careful!)
The second thread will share data,open files, signal handlers and signal dispositions, current working directory, user and group ID's. The new thread will get its own stack, thread ID, and registers though.
Continuing the analogy; your program (process) grows a second arm when it creates a new thread, connected to the same brain.
Solution 2
On Linux, the system call clone
clones a task, with a configurable level of sharing.
fork()
calls clone(least sharing)
and pthread_create()
calls clone(most sharing)
.
forking costs a tiny bit more than pthread_createing because of copying tables and creating COW mappings for memory.
Solution 3
You should look at the clone
manpage.
In particular, it lists all the possible clone modes and how they affect the process/thread, virtual memory space etc...
You say "threads easier to handle in code": that's very debatable. Writing bug-free, deadlock-free multi-thread code can be quite a challenge. Sometimes having two separate processes makes things much simpler.
Related videos on Youtube
srinathhs
Software developer at HP Sotorage. Working on : HTML5, CSS3, Adobe Flex, Java. facebook.com/srinathhs
Updated on March 17, 2020Comments
-
srinathhs about 4 years
What is the basic difference between a pthread and fork w.r.t. linux in terms of implementation differences and how the scheduling varies (does it vary ?)
I ran strace on two similar programs , one using pthreads and another using fork, both in the end make clone() syscall with different arguments, so I am guessing the two are essentially the same on a linux system but with pthreads being easier to handle in code.
Can someone give a deep explanation?
EDIT : see also a related question
-
Matt Joiner about 13 yearsI listed some useful differences here: stackoverflow.com/questions/3609469/…
-
-
srinathhs about 13 yearsSo, In the end linux handles both pthreads and fork in the same way and schedules them in the same way?
-
Mat about 13 yearsyes in general. this does not mean that you can't have different scheduling policies, or that a specific scheduler could apply different settings to thread groups vs plain old processes. (
fork
is implemented via theclone
syscall btw.) -
Matthieu almost 10 years+1 for pointing at the most important difference in my opinion: memory is shared with the process, whereas
fork
will live its own life. -
Sid Sahay about 6 yearsGrowing a second arm is a great analogy!
-
Farshid Ashouri almost 5 yearsThat's why God created Erlang.