What Is Process?
In operating system (OS), a process is the instance of a computer program that is being executed by one or many threads. For example, when we write a program in C or C++ and compile it, the compiler creates binary code. The original code and binary code are both programs, and therefore when we run the binary code, it becomes a process. Depending on the operating system, a process may be made up of multiple threads of execution that execute instructions concurrently.
Contexts Or Attributes Of A Process/ Context Of The Process
- Process Id: A unique identifier assigned by the operating system (OS).
- CPU Scheduling Information: For example, priority (Different processes may have different priorities for example a short process may be assigned a low priority in the shortest job first scheduling).
- Process State: Can be ready, running etc.
- I/O Status Information: For example, devices allocated to the process.
- CPU registers: Like the program counter (CPU registers must be saved and restored when a process is swapped in and out of CPU).
- Accounts Information
Process memory is divided into four sections for efficient working. The sections include:
- The stack, which is used for local variables when they are declared.
- The heap, which is used for the dynamic memory allocation, and is managed through calls to new delete, malloc, free etc.
- Text Section, which is made up the of the compiled program code, read in from non-volatile storage when the program is launched.
- The Data Section, which is made up of the global and static variables, allocated and initialized prior to executing the main.
What You Need To Know About Process
- The process can be referred to as program in execution.
- Processes require more time for context switching as they are heavier.
- Processes possess their individual program counter (PC), stack space and register set.
- Processes are wholly independent and run in a different memory space than the thread.
- All the different processes are treated separately by the operating system.
- Individual processes are independent of each other.
- Processes require more time for termination.
- The communication that takes place between the processes needs some time.
- In case any process gets blocked, the remaining processes carry on with their work.
- Processes require more resources than threads.
- Processes require more time to create.
- Different process has different copies of code and data.
- Processes do not require synchronization.
What Is Thread?
A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables and a stack which contains the execution history. Because threads have some of the properties of processes, they are sometimes referred to as lightweight processes.
An operating system (OS) that has thread facility, the basic unit of CPU utilization is a thread. A thread is made up or consists of a program counter (PC), a register and a stack space. Threads are not independent of one another like processes as a result threads share with other threads their code section, data section, OS resources also referred to as task, such as open files and signals. Threads can be divided into two types: user level thread (ULT) and kernel level thread (KLT).
- User Level Thread (ULT)- they are implemented in the user level library, they are not created using system calls.
- Kernel Level Thread (KLT)– KLT is a type of thread that knows and manages the threads. Instead of thread table in each process, the kernel itself has thread table (a master one) that keeps track of all the threads in the system. It also maintains the traditional process table to keep track of the processes.
What You Need To Know About Thread
- Thread is part of process.
- Threads require less time for context switching as they are lighter than processes.
- A thread shares the code section, address space, data section with the other threads.
- A thread runs in the same memory space as the process it belongs to.
- All user level peer threads are treated as single task by the operating system.
- Threads are parts of a process and so are dependent.
- Threads require less time for termination.
- The communication taking place between threads requires lesser time than processes.
- In case a user level thread experiences a blockage, the remaining threads also get restricted as the threads are considered to be a singular task by an operating system.
- Threads require fewer resources when compared to threads.
- Threads require less time to create.
- Sharing same copy of code and data can be possible among different threads.
- Threads require synchronization to avoid unexpected scenarios.
Process Vs. Thread In Tabular Form
|BASIS OF COMPARISON||PROCESS||THREAD|
|Description||The process can be referred to as program in execution.||Thread is part of process.|
|Context Switching Time||Require more time for context switching as they are heavier.||Require less time for context switching as they are lighter than processes.|
|Sharing Of Components||Processes possess their individual program counter (PC), stack space and register set.||A thread shares the code section, address space, data section with the other threads.|
|Independence||Processes are wholly independent and run in a different memory space than the thread.||A thread runs in the same memory space as the process it belongs to.|
|Dependence||Individual processes are independent of each other.||Threads are parts of a process and so are dependent.|
|Termination Time||Processes require more time for termination.||Threads require less time for termination.|
|Communication Time||The communication that takes place between the processes needs some time.||The communication taking place between threads requires lesser time than processes.|
|Effect Of Blockage On Other Processes/Thread||In case any process gets blocked, the remaining processes carry on with their work.||In case a user level thread experiences a blockage, the remaining threads also get restricted as the threads are considered to be a singular task by an operating system.|
|OS Resources||Processes require more resources than threads.||Threads require fewer resources when compared to threads.|
|Creation Time||Processes require more time to create.||Threads require less time to create.|
|Copies Of Data And Code||Different process has different copies of code and data.||Sharing same copy of code and data can be possible among different threads.|
|Synchronization||Processes do not require synchronization.||Threads require synchronization to avoid unexpected scenarios.|
What Are Some Of The Similarities Between Threads And Processes?
- Just like processes, threads share CPU and only one thread active (running) at a time.
- Threads within a process executes sequentially, this is also the case with processes.
- As is the case with process, if one thread is blocked, another thread can run.
- Threads just as is with the case of processes can create children.