Today I’d like to discuss about threads and process. Let’s take a look at a thread and a process actually means.
Referring to Microsoft’s documentation about threads and processes:
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
While a thread can be described as follows:
A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread’s set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread’s process. Threads can also have their own security context, which can be used for impersonating clients.
Processes are individual sequence execution. They run in separate memory space. While threads share the same memory space.
If the above description has gone over your head, here is a simple analogy:
Let’s consider a newspaper. There are different sections in a newspaper like sports section, job advertisement section, shares section etc.
- One can consider the newspaper as a program.
- A person reads a particular section in newspaper can be considered as a thread.
- Two or more people can share a single newspaper to read different sections. Hence there are multiple threads running in a program at same time.
- When nobody is reading a newspaper, then we can say that the program is not alive. So, we need a process to make it alive. In order to make the program alive, there should be at least one thread parsing in some part of that program.
- Hence process is a unification of a program and all the threads running into it.
- Each person reading a particular section in newspaper takes data from a single resource (the newspaper itself). Hence threads share resources of a process.
The above analogy is well explained in Processes vs Threads video.
In terms of PC applications, let’s consider Microsoft Office. When you install the Office package, then it is a raw program or a block of code. It just consumes memory space and does nothing by itself. When you start some application (let’s say Microsoft Word), then it executes a separate process with primary thread running into it. Different threads starts running when user performs different operations into it.
From the above descriptions, we can simply state the following:
Process: Program under execution is a process.
Thread: Concurrency within a program is handled by thread.
Hope that this post helps you clear out basic difference about threads and processes.
Upcoming: SystemVerilog: Fine grain process control.