Process vs Threads | Most Popular Interview Question

Опубликовано: 24 Декабрь 2022
на канале: Kishan Kumar
818
36

------------------------------------------------------------------------------
PROCESS
------------------------------------------------------------------------------
In simple terms, a process is a program in execution. A program is a set of instructions the computer needs to follow to achieve some task.
For e.g. there can be a program to compute the nth fibonacci.

When that program is loaded into the memory and is picked up by the processor, it is then called a Process.
For a process to run, it needs resources, such as CPU cycles, heap, stack, or some I/O.

A process is a self-contained execution environment that has its own memory space, system resources, and program code.
In this way, the process is isolated from the other process. One process cannot corrupt the memory address of another process.

----------------------------------------------------
THREADS
----------------------------------------------------
Now, let’s come to the Thread.
Threads belong to a process. A process has at least one thread called the main thread.

A thread is a lightweight process or a separate flow of execution within a program.
Threads are useful because they allow a program to perform multiple tasks concurrently or in parallel.

Like a process, each thread has its own registers and stack, but they share a common heap and memory space inside the process.
Unlike processes, threads can corrupt another thread execution within a process. There is no isolation concept for threads. This means one unresponsive thread can make the whole process unresponsive.