Process and Thread are the essentially associated. The process is an execution of a program whereas thread is an execution of a program driven by the environment of a process. Another major point which differentiates process and thread is that processes are isolated with each other whereas threads share memory or resources with each other.
Process
Programs are not stored on the primary memory in your computer. They are stored on a disk or a secondary memory on your computer. They are read into the primary memory and executed by the kernel. A program is sometimes referred as passive entity as it resides on a secondary memory.
Process is an executing instance of a program that performs the relevant actions specified in it. The operating system creates, schedules and terminates the processes. 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) which contains all the information regarding to a process such as a process id, priority, state and contents CPU register. PCB is a kernel-based data structure which contains information about
- 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.
Properties of a Process:
- Creation of each process require system calls.
- 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.
Dispatching
- Computers can execute multiple threads simultaneously. Each processor chip typically contains multiple cores.
- Many modern processors support hyperthreading i.e. each physical core behaves as if it is actually two cores, so it can run two threads simultaneously.
- At any given time, most threads do not need to execute (they are waiting for something).
- OS uses a process control block to keep track of each process.
- Execution state for each thread (saved registers, etc.)
- Scheduling information
- Information about memory used by this process
- Information about open files
- Accounting and other miscellaneous information
- At any given time a thread is in one of 3 states i.e. Running, Blocked (waiting for an event like disk I/O, incoming network packet, etc.) or Ready (waiting for CPU time)
- Dispatcher run a thread for a while, save its state and load state of another thread. It also does context switch i.e. changing the thread currently running on a core by first saving the state of the old process, then loading the state of the new thread.
Process Creation
Operating system creates a process by loading code and data into memory i.e.
- Create and initialize process control block.
- Create first thread with call stack.
- Provide initial values for “saved state” for the thread
- Make thread known to dispatcher; dispatcher “resumes” to start of new program.
Thread
Thread is the smallest executable unit of a process. For example, when you run a notepad program, operating system creates a process and starts the execution of main thread of that process.
A process can have multiple threads. Each thread will have their own task and own path of execution in a process. For example, in a notepad program, one thread will be taking user inputs and another thread will be printing a document.
All threads of the same process share memory of that process. As threads of the same process share the same memory, communication between the threads is fast.
Thread vs Process
Difference between Thread and Process from operating system perspective:
- 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. Communication among thread is cheaper than inter-process communication. - 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. - Existence
A thread cannot exist without process. Also, a process, at least, has one thread to do the job e.g. main thread for Java programs. - Context Switching
Since all threads from the same process share same address space, inter-thread communication and context switching between them are much faster than inter-process communication and context switching between process.