Process vs Thread

Process vs Thread
what's the difference between process and thread?
This is a common interview question.
That's take a look.
To better answer this question.
Let's first understand what a program is?
A program is an executable file.
It contains the code or a set of processing instructions that is stored as a file on disk.
When the code in the program is loaded into memory and executed by processor, it becomes a process. An active process also includes the resources the program needs to run. These resources are managed by the operating system.
Some examples of processor registers ,program counters, stack pointers, memory pages assigned to the process for his heap and stack etc.
there is an important property of a process that is worth mentioning, each process is his own memory address space.
One process cannot corrupt the memory space of another process. This means that when one process malfunctions ,other processes keep running.



Chrom is famous for taking advantage of this
process isolation by running each Tab in its own process when one Tab misbehaves due to a bug or a malicious attack other tabs are unaffected.
Now, what is a threat?

A threat is a unit of execution within a process. A process has a at least one threat, it is called a Main Thread. It is not uncommon for a process to have many threats, each threat has his own stack, earlier we mention registers , program counters, stack pointers
as being part of a process.

It is more accurate to say that those things belong to a threat.

Threads within a process share a memory address space , it is possible to communicate
between threats using that share memory space . However one misbehave thread could bring down the entire process.
How does the operating system run the thread or process?

On the CPU, this is handle by context switching , during a context switch, one process is switch out of the CPU , so another process can run .
The operating system,stores the states of the current running process , so the process can be restore and resume execution at a later point, it then restores the previously safe states of a different process and resumes execution for that process.
contact switching is expensive, it involves saving and loading of registers ,switching our memory pages and updating various kernel data structures


Switching execution between threads also requires context switching

it is generally faster to switch context between threads than between processes .
There are fewer stage to track and more importantly

since threads share the same memory address space, there is no need to switch our virtual memory pages which is one of the more expensive operation during a context switch.

Context switching is so costly, there are other mechanisms to try to minimize it.
Some examples of fibers and coroutines

For even lower context switching cost , in general , they are cooperatively scheduled that is they must you control for other to run .
In other words, the application is self handles task scheduling. It is a responsibility of the application to make sure a long running task is broken up by uting perviodiclally .