DEV Community

Cover image for Process Management (Process vs Thread)
Mehul Sharma
Mehul Sharma

Posted on

Process Management (Process vs Thread)

Today I am trying to cover the very common but still confusing topic in process management i.e. Process vs Thread.

So as per me before jumping directly onto the process and thread difference first we should understand what these two terms are and I will also try to explain the difference side by side.

Process: Most common definition for process is "The process can be thought of as a program in execution", or in simple words we can say that if we have any program or binary that is running at that time then we will call it a process. Or we can take an example of objects in OOPs concepts to make it easier. Like we can create multiple objects for a single class, same we can create multiple processes for a single program but all processes will be having different PIDs.
To test with we can take a very simple example where we can open two terminals and open vim editors and then we will see that both will be having different PIDs.

Alt Text

In the above example we can see that we have different PIDs for both processes while we are running the same program.
For process we can say that these will be exact clones of the same program if we will be having a child process of any process.

Thread: For thread we can say that "A thread is the unit of execution within a process. A process can have anywhere from just one thread to many threads".
I am hoping that the above statement is enough to explain about the threads but still I will try to explain this bit more. So we can say that threads are multiple part of processes but the difference is these are part of the process so these threads will be having the same PID as the process for all threads as they do not clone the process but they use the same data for all the threads within one process they are connected. We can see the example to threads with the help of ps command with the combination of a few flags.
Alt Text

In the above example we took docker threads and we can see that all threads are having the same PID but still they are having different threadIDs.

Difference between process and threads:
Process = Program + State of all threads executing in the program

The simplest example that I can think of to understand this completely is the Daily News-Paper. We can say that the entire news paper is a process and multiple sections like sports or business are threads of the process.

I am hoping that I was able to explain this topic properly but still feel free to correct me in case I am wrong anywhere.

Top comments (5)

Collapse
 
bluma profile image
Roman Diviš

Inexperienced users may not know what is in the screenshots. As there is missing header line and PIDs arent highlighted. Also PID and PPID are not explained or mentioned. TGID is not thread id but thread group id, which is not the same. In details you say that processes will have different pids and ppids, only pids will be always different. What I think that this description is missing is information that process has its memory and its shared between threads, but threads have their memory and it is usually private for them (yes its part of process memory but you arent gonna to share stack between two threads)

Collapse
 
mehulsharma20 profile image
Mehul Sharma

Thanks for your valuable feedback and yes you are correct and I will try to make an update in this post soon.
Really appreciate...

Collapse
 
felipeschossler profile image
Felipe Schossler

Finally I understood the topic. Really thank you about this!

Collapse
 
mehulsharma20 profile image
Mehul Sharma

Really a motivation for next one, Thanks

Collapse
 
nitishtiwari profile image
Nitish Tiwari

Precise, I'd say. Keep writing.