Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. 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.
  2. Threads are light-weight process which have less overhead

But, there are still some questions in my mind.

  1. When should you prefer fork() over threading and vice-verse?
  2. If I want to call an external application as a child, then should I use fork() or threads to do it?
  3. 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?
  4. Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?
like image 215
Rushabh RajeshKumar Padalia Avatar asked May 03 '13 08:05

Rushabh RajeshKumar Padalia


People also ask

Should I thread or fork?

Forking is much safer and more secure because each forked process runs in its own virtual address space. If one process crashes or has a buffer overrun, it does not affect any other process at all. Threads code is much harder to debug than fork.

What does forking a thread mean?

The fork( ) system call creates an exact duplicate of the address space from which it is called, resulting in two address spaces executing the same code. Problems can occur if the forking address space has multiple threads executing at the time of the fork( ).

What happens when you fork a thread?

fork creates a new process. The parent of a process is another process, not a thread. So the parent of the new process is the old process. Note that the child process will only have one thread because fork only duplicates the (stack for the) thread that calls fork .

What is the purpose of forking?

Fork() is used to create new processes as every body has written. Show activity on this post. fork() system call creates the exact duplicate of parent process, It makes the duplicate of parent stack, heap, initialized data, uninitialized data and share the code in read-only mode with parent process.


2 Answers

The main difference between forking and threading approaches is one of operating system architecture. Back in the days when Unix was designed, forking was an easy, simple system that answered the mainframe and server type requirements best, as such it was popularized on the Unix systems. When Microsoft re-architected the NT kernel from scratch, it focused more on the threading model. As such there is today still a notable difference with Unix systems being efficient with forking, and Windows more efficient with threads. You can most notably see this in Apache which uses the prefork strategy on Unix, and thread pooling on Windows.

Specifically to your questions:

When should you prefer fork() over threading and vice-verse?

On a Unix system where you're doing a far more complex task than just instantiating a worker, or you want the implicit security sandboxing of separate processes.

If I want to call an external application as a child, then should I use fork() or threads to do it?

If the child will do an identical task to the parent, with identical code, use fork. For smaller subtasks use threads. For separate external processes use neither, just call them with the proper API calls.

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?

Not entirely sure but I think it's computationally rather expensive to duplicate a process and a lot of subthreads.

Is it True that fork() cannot take advantage of multiprocessor system because parent and child process don't run simultaneously?

This is false, fork creates a new process which then takes advantage of all features available to processes in the OS task scheduler.

like image 125
Niels Keurentjes Avatar answered Oct 18 '22 11:10

Niels Keurentjes


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:

  1. A forked process is considered a child process whereas a threaded process is called a sibling.
  2. 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.
  3. Process switching requires the help of OS but thread switching it is not required
  4. Creating multiple processes is a resource intensive task whereas creating multiple thread is less resource intensive task
  5. Each process can run independently whereas one thread can read/write another threads data. Thread and process lecture enter image description here
like image 38
Selvaperumal Avatar answered Oct 18 '22 12:10

Selvaperumal