Why use multiple processes instead of multiple threads?

1 answer

Answer

1126660

2026-04-11 23:40

+ Follow

Linux
Linux

You use a process when you want a separate program, and a thread when you want to asynchronously execute some different code contained within the same program.

A process is an address space containing instructions, data, stack, etc. It represents one load module (or program) loaded into memory, ususally by the operating system's exec or equivalant call.

There can be more than one process loaded from the same load module. They are separate and distinct, even though they might share regions of instructions and constant data. As an example, the execution of ksh (in Linux) or cmd (in Windows) represents a process.

While executing, a process can invoke another process, either a copy of itself or a different one. For example, ksh can invoke ls, and cmd can invoke explorer. Keep in mind that this is still a different address space. In the case of Linux, the process actually makes a copy of itself (fork) and then overlays itself with the new load module.

A thread, on the other hand is an execution path through a process. Every process has at least one thread, which starts with the first instruction it executes after being loaded, and ends with the call to the exit or equivalent operating system call.

So, to clarify, what executes is actually called a thread, and the process is just the address space. Different naming conventions do exist - this is the Windows (and some other OS's) convention.

{Restating from three paragraphs before} While executing, a thread can load another process, either a copy of its containing process or a different one, or it can invoke a new thread within itself. While the difference might seem slight, it is not, because the (now two) threads share the same address space, and they can easily communicate with each other, assuming appropriate synchronization is used.

In the case of Linux, you could say that a separate process is started as fork followed by overlay, while a new thread started as fork without the overlay.

Each thread has its own copy of its local variables, and a copy of the invoking parameters. In the simple case, that is sufficient. In the more complex case, involving explicitly allocated heap memory, either different heaps are used, or a mechanism for synchronized sharing is implemented.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.