A process is when a program loads into the memory, along with its required resources, and starts executing. In other words, it is a program in execution.
A process control block controls the process operations and contains critical information about the process such as the process ID, state, registers, counter, etc.
A process table maintains information about each process in the form of a context block to facilitate process scheduling and context switching.
Processes can communicate with each other via two methods:
In shared memory, the process has access to a shared memory region. One process writes to that shared memory and the other processes can access or read information from the same memory region.
In message passing, processes can read and write to a message queue without sharing any memory or variables. The messages are sent and received by processes through system calls.
A thread is a subset of a program, and a single program can have multiple threads. All the threads of a program share the same address space in memory.
Threads of the same process can communicate with each other through synchronization primitives like locks and semaphores, events like wait and notify, or through shared memory.
The following illustration shows how a process (single-threaded) and a multi-threaded process share memory.
The table below summarizes the differences between processes and threads.
Processes | Threads |
---|---|
Processes are heavyweight | Threads are lightweight |
Each process has a separate memory space | Each thread shares the memory with its parent process |
Inter-process communication is slower | Inter-thread communication is faster |
Context switching takes more time | Context switching takes less time |
Processes do not share memory | Threads share memory with other threads of the parent process |