Process vs. Threads
Process vs. Threads
Definition and Differences explained
To understand the differences between
processes and threads, one should know the basics about both of them.
What is a process?
A process is just the name given to
programs that have been loaded into the memory (RAM) for a purpose and managed
by the operating system while being executed by the CPU. A program is a set of
instructions and a process is the execution of those instructions.
If you relate the real life to OS, a process is like “Baking a cake”, and here the program is the “cake recipe”, the input is the cake ingredients and the output is the cake.
So a process contains a program, input, output and a state. If the same program is running twice (E.g.: a text editor started twice), they count as 2 processes and the OS shares the code between them.
If you relate the real life to OS, a process is like “Baking a cake”, and here the program is the “cake recipe”, the input is the cake ingredients and the output is the cake.
So a process contains a program, input, output and a state. If the same program is running twice (E.g.: a text editor started twice), they count as 2 processes and the OS shares the code between them.
How a process is created?
There are 4 ways generally known to create
processes.
1. System initialization
Whenever
an OS is booted (E.g.: When you start-up your PC), several processes are
created and some of them are in the foreground (Visible on the screen), while
most of them are running in the background (called “Daemons”). Anyone can view all these processes through
task manager in Windows OS.
2. Created by a running process
Whenever
a running process needs help to finish its job, it issues a system call to
create 1/more processes.
3. Created by user request
By
double-clicking an icon (in windows), or by typing a command (in Linux).
4. Created in Batch jobs.
In
mainframe computers, when the OS has enough resources to run another job, it
creates a new process and runs the job.
Even though these are separated in all these 4 ways, a process is commonly created by the running process issuing a system call to create another process.
Note:
System call happens when a process requests a service from the kernel part of the system.
If a process can be created, it can be
terminated too.
How to destroy a process?
There are 4 ways to terminate a process.
1.Normal exit(Voluntary)
A
process voluntarily exits, when it has finished its job.
2.Error exit(Voluntary)
A
process voluntarily exits when it detects a program bug or when there is an
illegal instruction present in its program(Eg: dividing an integer by 0).
3.Fatal error(Involuntary)
When
the compiler is asked to compile a non-existent file, that process is
terminated.
4.Killed by another process(Involuntary)
The
killer process which has a proper authorization to kill, can terminate another
process.
The states of processes
1.Running to Blocked
When
a running process needs an input from another process, this process is blocked
until the input is obtained.
2.Running to Ready
The scheduler selects the processes from
the queue and loads them into the memory. A process goes from Running to Ready,
when the scheduler decides that the current process has run enough and gives
the CPU to another ready-to-run process.
3.Ready to Running
When the scheduler selects the next the
process in queue.
4.Blocked to Ready
When the input of the blocked process
becomes available, the process becomes Ready to run.
Process Hierarchies
In Unix:
Parent
processes stay at the top of the hierarchy and the child processes can produce
more child processes.
In windows:
No
defined hierarchy as Unix. But the Parent has a token called “handle”, and it
can be passed along to any process. So, there IS a process hierarchy, but not a
constant one.
Now that we know about Processes, let’s see about Threads.
What is a Thread?
Threads are the mini-light weight processes
inside a process.
Why Threads?
1. Since threads are lighter than the
processes, it’s easier to create a thread than a process. They even contain
programs, inputs, outputs and states just like processes.
2. Each process has a separate address space
(A list of memory locations, which the process can read or write), but the
threads inside a process share the 01 address space of that process. For
example, using 3 processes to finish a job requires 3 address spaces. But
instead, using 1 process with 3 threads require only 01 address space, which
reduces memory wastage.
3. No need to have protection between
threads, because threads of a process are not hostile to each other, even
though processes can be hostile with each other.
4. Even if the Classical Thread Model
(Single-threaded) can be sluggish now-a-days, multi-threading enhances
performance, by starting out as single-threaded and then creating threads
according to the need.
5. Each of the threads has a stack of its
own.
Differences between Processes and Threads
Process |
Threads |
Used to group the resources together
|
Entities scheduled for the execution of the CPU
|
Processes each has an address space and runs in
separate memory spaces.
|
Threads share the address space of the process and
runs in shared memory spaces.
|
Multiple processes run in a system.
|
Multiple threads run inside a process.
|
Processes share Memory and other resources with
other processes.
|
Threads share the resources of the process.
|
CPU switches between processes to run.
|
CPU switches between threads to run.
|
Independent.
|
Dependent on other threads of the same process.
|
Need to use Inter-Process Communication (IPC) to
communicate among processes.
|
Threads can directly communicate with the other
threads of its process.
|
Expensive to create and destroy.(because of system
calls.)
|
Light weight and easy to create and destroy.
|
Only the parent process can control its child
processes.
|
All threads can control any threads inside its
process and modify their content.
|
Hope it helps you well...!
Image courtesy:
In a multi processor system how is the thread is spread into the several processor
ReplyDelete