Forking vs Threading

59,924

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
59,924

Related videos on Youtube

Rushabh RajeshKumar Padalia
Author by

Rushabh RajeshKumar Padalia

Self-Motivated, Team Player, Qualified and Ambitious software developer with a passion for developing innovative software solutions. I am international student currently pursuing Master of Computing degree with a Major in Software Engineering at Australian National University. I also have an undergraduate in Information Technology from James Cook University. Through out my universities I have maintained a good score especially in programming and other Software Engineering Courses. I also do freelancing work to develop software solutions for my clients which improves my communication and solving and analytical abilities. This also adds to my experience to develop and maintain online resources. I have worked in diverse teams during university and freelancing projects. My freelancing projects have allowed me to work independently and multitask. I also have to follow strict deadlines and I am not afraid to learn new technologies and techniques as an when required. I have self-taught many technologies when it is required and I have constantly updated my working techniques from my continuous learning. I maintain all my project in Github(https://github.com/PadaliaRushabh) . Also checkout my website (http://rushabh.padalia.net) which show my recent works. I have one more semester to go at ANU and I am looking for jobs with positions of Junior Software developer, Junior Software Engineer, Graduate Mobile Developer, Graduate Web Developer. If you have an graduate opening and if you feel I am suited for the position please contact me through gmail ([email protected]) or phone (+61 452403830). I would be happy to provide you with all the required information

Updated on February 22, 2020

Comments

  • Rushabh RajeshKumar Padalia
    Rushabh RajeshKumar Padalia about 4 years

    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?
  • ibid
    ibid almost 11 years
    On Unix, the proper API for invoking an external program is fork followed by exec (with a host of additional details I'm omitting here). For some special cases, there are simpler alternatives (mainly popen).
  • RaGa__M
    RaGa__M over 4 years
    I don't think how did it get lots of votes without asking some questions. 1)A forked process is considered a child process whereas a threaded process is called a sibling.-- this point is irrelevant, machine doesn't care whether you are a child or sib. 2)Process switching requires the help of OS but thread switching it is not required-- it does, unless you use something called user-land threads. 3)Creating multiple processes is a resource intensive task whereas creating multiple thread is less resource intensive task-- depends on the implementation.
  • 12431234123412341234123
    12431234123412341234123 over 3 years
    On Linux and many other systems, a process is not significant heavier than a process. Thanks to COW the code and much of the heap and stack is shared between child and parent. A forked process can share things with the parent, for example a process can request memory that will be shared with future children.
  • 12431234123412341234123
    12431234123412341234123 over 3 years
    fork() only copies the calling thread. It is not expensive since fork() uses COW. Using threads when you also want to use fork() without following it directly by exec*() is often a bad idea, since other threads may hold locks that can't be released in the child when the other treads suddenly stop.