Monday, September 4, 2017

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. See the difference between Stack and heap for more details.

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. cancelation, 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.

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.

The process only has control over child process but the thread has a greater degree of control over other threads of the same process.
Difference between thread and process in Java


No comments:

Post a Comment