One of the common question from programming interviews is, what is the difference between a Thread and a Process? Well, the main difference between them is that a Process is a program which is executing some code and a thread is an independent path of execution inside the process. A process can have more than one thread for doing independent task e.g. a thread for reading data from disk, a thread for processing that data and another thread for sending that data over the network. This technique to improve throughput and better utilize CPU power is also known asmulti-threading. Technically, most significant difference between thread is address space and context switching. All thread from a process share same address space but a process has their own address space. Similarly, context switching between process is more expensive than context switching between threads. These were some fundamental difference between thread and process which every programmer should know. If you don’t then I suggest you must read Java Threads By Scott Oaks , one of the great introductory book on multithreading.
Thread vs Process
Let’s analyse some more difference between a Thread and a process in a programming language like Java or from operating system perspective:
All threads from the same process share memory space of the process that created it, on the other hand, each process have theirown address space.
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.
Threads have less overhead compared to Process in terms of metadata, context switch and CPU and memory requirement.
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.
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.
Changes to the parent thread e.g. cancellation, 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.
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
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 andcontext switching between process.
Both Thread and process shares lot of similarity in the sense that both executes code, both has ID, execution time, based priority and exit status. Both can create another process and thread as well, but thethread has greater degree of control over other threads of the same process. The process only has control over child process.
There is a saying that one image is equal to 1000 words, here is one diagram which will help you learn the difference between thread and process clearly:
That’s all about the difference between a Thread and process in the operating system and Java or any other programming language. Threads are lightweight, an independent path of execution inside the process. They share same address space of the process and less overhead compared to process. Process are an individual program, require their own address space and has more overhead than threads. Also, the difference between Thread and process depends upon operating system implementation. For example, there is very less difference between a thread and a process in Linux than Windows.