What are the differences between processes and threads?
Threads are lightweight, an independent path of execution in the process. They share same address space of the process and less overhead compared to process.
Process are an individual program, require their own address space and has more overhead than threads.
Also, the difference between Thread and process depends upon operating system implementation. For example, there is very less difference between a thread and a process.
A process consists of multiple threads. A thread is a smallest or least part of the process which can be able to execute concurrently with other parts(threads) of the process.
Thread vs Process
- Address Space: All threads from the same process share memory space of the process that created it, on the other hand, each process has their own address space.
- Communication: Threads can directly communicate with other threads of the same process. Many programming language e.g. Java provides tools for inter-thread communication. It’s much cheaper than inter-process communication, which is expensive and limited.
- Overhead: Threads have less overhead compared to Process in terms of metadata, context switch and CPU and memory requirement.
- Data Segment: Thread have direct access to the data segment of its process, an individual process has their own copy of the data segment of the parent process.
- Creation: New threads can be easily created e.g. in Java you can create a new Thread by creating an object of Thread class and calling start() method on it, while new process requires duplication of the parent process.
- Behavior: Changes to the parent thread e.g. cancellation, priority, the daemon may affect the behavior of the other threads of the process, but changes to the parent process do not affect child process.
Both Thread and process shares lot of similarity in the sense that both executes code, both has ID, execution time, based priority and exit status. Both can create another process and thread as well, but the thread has a greater degree of control over other threads of the same process. The process only has control over child process.
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
A thread is an entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread’s set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread’s proc
The process is the execution of a program and performs the relevant actions specified in a program, or it is an execution unit where a program runs. The operating system creates, schedules and terminates the processes for the use of the CPU. The other processes created by the main process are known as child process.
A process operations are controlled with the help of PCB(Process control Block) can be considered as the brain of the process, which contains all the crucial information regarding to a process such as a process id, priority, state, PWS and contents CPU register.
PCB is also a kernel-based data structure which uses the three kinds of functions which are scheduling, dispatching and context save.
- Scheduling – It is a method of selecting the sequence of the process in simple words chooses the process which has to be executed first in the CPU.
- Dispatching – It sets up an environment for the process to be executed.
- Context save – This function saves the information regarding to a process when it gets resumed or blocked.
There are certain states included in a process lifecycle such as ready, running, blocked and terminated. Process States are used for keeping the track of the process activity at an instant.
From the programmer’s point of view, processes are the medium to achieve the concurrent execution of a program. The chief process of a concurrent program creates a child process. The main process and child process need to interact with each to achieve a common goal.
Interleaving operations of processes enhance the computation speed when i/o operation in one process overlaps with a computational activity in another process.
Properties of a Process:
- Creation of each process includes system calls for each process separately.
- A process is an isolated execution entity and does not share data and information.
- Processes use IPC (Inter-process communication) mechanism for communication which significantly increases the number of system calls.
- Process management consumes more system calls.
- Each process has its own stack and heap memory, instruction, data and memory map.
The thread is a program execution that uses process resources for accomplishing the task. All threads within a single program are logically contained within a process. The kernel allocates a stack and a thread control block (TCB) to each thread. The operating system saves only the stack pointer and CPU state at the time of switching between the threads of the same process.
Threads are implemented in three different ways; these are kernel-level threads, user-level threads, hybrid threads. Threads can have three states running, ready and blocked; it only includes computational state not resource allocation and communication state which reduces the switching overhead. It enhances the concurrency (parallelism) hence speed also increases.
Multithreading also comes with demerits, Multiple threads doesn’t create complexity, but the interaction between them does.
A thread must have priority property when there are multiple threads are active. The time it gets for execution respective to other active threads in the same process is specified by the priority of the thread.
Properties of a Thread:
- Only one system call can create more than one thread (Lightweight process).
- Threads share data and information.
- Threads shares instruction, global and heap regions but has its own individual stack and registers.
- Thread management consumes no or fewer system calls as the communication between threads can be achieved using shared memory.
- The isolation property of the process increases its overhead in terms of resource consumption.