The main difference between process and thread is that process is a program in execution while a thread is a small execution unit in a process.
A program is a set of instructions to perform a certain task. A process is a program in execution. It is not a program but it is more than that. In other words, a program is a passive entity whereas a process is an active entity. When the program is executed, it becomes a process. On the other hand, a thread is a small execution unit that belongs to a process. Processes are heavyweight but threads are lightweight. Therefore, a single process is divided into multiple threads for efficiency.
Key Areas Covered
1. What is Process
– Definition, Functionality
2. What is Thread
– Definition, Functionality
3. Relationship Between Process and Thread
– Outline of Association
4. Difference Between Process and Thread
– Comparison of Key Differences
Process, Thread, Operating System
What is a Process?
A process is a program in execution. When the programmer writes a program and executes it, that program becomes a process. It performs the tasks according to the instructions of the program.
When a process loads to the memory, it further divides into four segments. They are the stack, heap, text and data. The stacks store the temporary data such as function parameters and local variables. The heap dynamically allocates memory to a process at runtime. The text section contains the content of the processor registers and the value of the program counter. The data section contains the static and global variables.
A process goes through several states. They are as follows.
New – The process creates at the new state
Ready – In the ready state, the process is waiting to be assigned to the processor so that it can run. A process can come to this state after the start state. Furthermore, when a scheduler assigns the CPU to a process while a different process is already executing, then that already executing process goes to the ready state.
Running – In the running state, the process is assigned to a processor and it executes the instructions.
Waiting – The waiting state reflects that the process is waiting for some event to occur; for example, waiting until a file is available or waiting until IO completion.
Terminating – The terminate state indicates that the process has finished execution. Now, it is possible to remove it from the main memory.
Moreover, an operating system maintains a Process Control Block (PCB) for each process. It contains information about the process such as process ID (PID), program counter, CPU registers, CPU scheduling information, memory management information and IO status information. PCB is deleted when the process terminates.
What is Thread
A thread is an execution unit and it contains program counter, stack and set of registers. It is a lightweight process. A thread cannot exist outside a process and each thread belongs to a process. There are two types of threads: user threads and kernel threads. User threads are user managed threads while kernel threads are supported and managed by the kernel.
In a computer system, it is not effective to create processes for each task because it requires more resources. Therefore, a process is divided into multiple sub-processes and each sub-process executes a subtask. This sub-process is a single unit in the process and it is called a thread. Dividing a process into multiple threads and these threads executing in parallel is called multithreading. Therefore, threads improve the application performance through parallelism.
Mainly, threads provide concurrency within a process. Furthermore, threads improve utilization in multiprocessor architectures for efficiency.
Relationship Between Process and Thread
- A thread is a lightweight process.
Difference Between Process and Thread
A process is an instance of a computer program that is being executed. A thread is a component of a process which is the smallest execution unit.
A process is heavyweight while a thread is lightweight.
A process switching requires interacting with the operating system. In contrast, thread switching does now require interacting with the operating system.
Each process has its own memory space. A process does not share memory with other processes. Threads use the memory of the process they belong to. Thus, threads share the memory with other threads of the same process.
A process requires more resources. A thread requires minimum resources.
It is difficult to create a process. It is easier to create a thread.
Inter-process communication is slow because each process has a different memory address. On contrary, inter-thread communication is fast because the threads share the same memory address of the process they belong to.
In a multi-processing environment, each process executes independently. But a thread can read, write or modify data of another thread.
The difference between process and thread is that the process is a program in execution while the thread is a small execution unit in a process. Process creation is difficult but thread creation is economical. Furthermore, processes are resource intensive while threads require minimum resources.