A process is the execution of a program that allows you to perform the appropriate actions specified in a program. It can be defined as an execution unit where a program runs. The OS helps you to create, schedule, and terminates the processes which is used by CPU. The other processes created by the main process are called child process.
Here are the important properties of the process:
Creation of each process requires separate system calls for each process.
It is an isolated execution entity and does not share data and information.
Processes use the IPC(Inter-Process Communication) mechanism for communication that significantly increases the number of system calls.
Process management takes more system calls.
A process has its stack, heap memory with memory, and data map.
A process in OS can remain in any of the following states:
NEW: A new process is being created.
READY: A process is ready and waiting to be allocated to a processor.
RUNNING: The program is being executed.
WAITING: Waiting for some event to happen or occur.
TERMINATED: Execution finished.
How it work?
When we start executing the program, the processor begins to process it. It takes the following steps:
Firstly, the program is loaded into the computer's memory in binary code after translation.
A program requires memory and other OS resources to run it. The resources such that registers, program counter, and a stack, and these resources are provided by the OS.
A register can have an instruction, a storage address, or other data that is required by the process.
The program counter maintains the track of the program sequence.
The stack has information on the active subroutines of a computer program.
A program may have different instances of it, and each instance of the running program is knowns as the individual process.
Thread is an execution unit that is part of a process. A process can have multiple threads, all executing at the same time. It is a unit of execution in concurrent programming. A thread is lightweight and can be managed independently by a scheduler. It helps you to improve the application performance using parallelism.
Multiple threads share information like data, code, files, etc. We can implement threads in three different ways:
Kernel-level threads: The kernel-level threads are handled by the Operating system and managed by its kernel. These threads are slower than user-level threads because context information is managed by the kernel. To create and implement a kernel-level thread, we need to make a system call.
User-level threads : the user-level threads are only managed by users, and the kernel does not have its information. These are faster, easy to create and manage. The user-level threads are implemented by user-level libraries, not by the system calls.
Here are important properties of Thread:
Single system call can create more than one thread
Threads share data and information.
Threads shares instruction, global, and heap regions. However, it has its register and stack.
Thread management consumes very few, or no system calls because of communication between threads that can be achieved using shared memory.
How it work?
As we have discussed that a thread is a subprocess or an execution unit within a process. A process can contain a single thread to multiple threads. A thread works as follows:
When a process starts, OS assigns the memory and resources to it. Each thread within a process shares the memory and resources of that process only.
Threads are mainly used to improve the processing of an application. In reality, only a single thread is executed at a time, but due to fast context switching between threads gives an illusion that threads are running parallelly.
If a single thread executes in a process, it is known as a single-threaded And if multiple threads execute simultaneously, then it is known as multithreading.
Difference Between Process and Thread
Process means program is in execution
Thread means segment of process
It takes more time to terminate
It takes less time to terminate
It takes more time for creation and context switching
It takes less time for creation and context switching
Process is isolated
Threads share memory
Process consumes more resources and is less efficient in terms of communication
Threads uses less resources and is more efficient in terms of communication.
If one process is blocked then it will not effect the execution of other process
Second thread in the same task couldnot run, while one server thread is blocked.
Process has its own Process Control Block, Stack and Address Space.
Thread has Parents’ PCB, its own Thread Control Block and Stack and common Address space.
Process switching uses interface in operating system.
Thread switching does not require to call a operating system and cause an interrupt to the kernel.
The Tech Platform