Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pthread and fork on gnu/Linux

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

like image 576
srinathhs Avatar asked Apr 01 '11 14:04

srinathhs


People also ask

What is the difference between thread and fork in Linux systems?

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.

Does Pthread use fork?

The pthread_atfork() function declares fork() handlers that are called before and after fork() in the context of the thread that called fork() . The prepare handler is called before fork() starts. The parent handler is called after fork() returns in the parent.

What is Pthread in Linux?

POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time.

Is fork same as thread?

Threads are functions run in parallel, fork is a new process with parents inheritance. Threads are good to execute a task in parallel, while forks are independent process, that also are running simultaneously.


2 Answers

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.

like image 51
Gabriel Fair Avatar answered Sep 28 '22 05:09

Gabriel Fair


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.

like image 26
Davood Hanifi Avatar answered Sep 28 '22 05:09

Davood Hanifi