So, guys, now going forward now why threads are called lightweight process?
It is to be noted that one of the most commonly termed used to address threads is a lightweight process.
Threads are often called as lightweight process.
Now what does this lightweight here actually means?
So a thread is called a lightweight process because of the following reasons.
When a thread is created, it uses almost all the pre-existing resources of the main thread.
Remember, when a process is newly created, at least one thread is created and that thread is main
thread.
When the main thread is created, then all the necessary resources which are required for the process
to exist are also created.
Right.
So it simply means that when a thread other than the main thread is created, it uses almost all the
pre-existing resources which were created along with the main thread and hence operating system don't
have to work too hard in order to create or delete an isolated execution environment of the new thread.
So for example, when the main thread is created, the page table of the process is created, right?
So when any other thread of the process is created, the page table need not be created again.
Similarly, all the shared libraries of the process are already loaded in the main memory.
Right.
So these need not be loaded again in the main memory when some other new thread of the process is created.
Similarly, if the process needs to communicate with some external entity via sockets, then it might
be possible that the main thread has already done the job.
It has already opened all the required sockets with the external entities, with which it wants to communicate.
So when the new thread is created, you need not be create any new socket again.
Right?
Contrary to this, when a thread dies.
Operating system don't have to clean up every resource used by the thread as those resources could still
be in use by other threads of the same process.
So example of such resources are, for example, heap memory of the process, the open sockets of the
process, the open files, the inter-process communication based techniques that the process is using.
So all these resources which is made available to the process need not be reclaimed back by the operating
system when the thread dies, right?
Because a process could be multithreaded and there could be any number of other threads which may be
using these resources of the process.
So operating system don't have to clean up every resource whenever the thread dies.
So it simply means that operating system don't have to work too hard when a thread is created or when
a thread dies.
And it is for this reason that threads are called lightweight processes.
Right?
And finally, because threads are light weighted processes, therefore the context switching which happens
between the threads which belong to the same process that is, for example, thread, t1 and thread
t two are the threads that belong to the same process.
Then the context switching between the threads from T1 to T2 and vice versa is called thread switching
and this thread switching is faster than the process switching.
Now process switching is nothing, but it is the context switching between the two threads.
But these two threads belong to the different processes.
Right.
So note the difference between thread switching and process Switching and thread switching is way faster
than process switching.
So I would suggest that Google these terms and read more about these terms and understand that why thread
switching is faster than process switching.
The answer lies in these two points.
Right?
The answer lies in the fact that threads are lightweight processes.