WEBVTT

00:04.910 --> 00:09.790
So, guys, now going forward now why threads are called lightweight process?

00:09.830 --> 00:17.090
It is to be noted that one of the most commonly termed used to address threads is a lightweight process.

00:17.180 --> 00:20.360
Threads are often called as lightweight process.

00:20.390 --> 00:23.880
Now what does this lightweight here actually means?

00:23.900 --> 00:28.160
So a thread is called a lightweight process because of the following reasons.

00:28.280 --> 00:34.670
When a thread is created, it uses almost all the pre-existing resources of the main thread.

00:34.700 --> 00:41.330
Remember, when a process is newly created, at least one thread is created and that thread is main

00:41.330 --> 00:41.980
thread.

00:41.990 --> 00:47.150
When the main thread is created, then all the necessary resources which are required for the process

00:47.150 --> 00:49.310
to exist are also created.

00:49.310 --> 00:50.060
Right.

00:50.900 --> 00:57.260
So it simply means that when a thread other than the main thread is created, it uses almost all the

00:57.260 --> 01:03.800
pre-existing resources which were created along with the main thread and hence operating system don't

01:03.800 --> 01:10.880
have to work too hard in order to create or delete an isolated execution environment of the new thread.

01:10.910 --> 01:18.050
So for example, when the main thread is created, the page table of the process is created, right?

01:18.050 --> 01:23.280
So when any other thread of the process is created, the page table need not be created again.

01:23.300 --> 01:28.790
Similarly, all the shared libraries of the process are already loaded in the main memory.

01:28.790 --> 01:29.570
Right.

01:30.290 --> 01:36.470
So these need not be loaded again in the main memory when some other new thread of the process is created.

01:36.500 --> 01:42.740
Similarly, if the process needs to communicate with some external entity via sockets, then it might

01:42.740 --> 01:45.740
be possible that the main thread has already done the job.

01:45.740 --> 01:52.390
It has already opened all the required sockets with the external entities, with which it wants to communicate.

01:52.410 --> 01:57.480
So when the new thread is created, you need not be create any new socket again.

01:57.480 --> 01:58.200
Right?

01:59.170 --> 02:02.080
Contrary to this, when a thread dies.

02:02.110 --> 02:08.200
Operating system don't have to clean up every resource used by the thread as those resources could still

02:08.230 --> 02:11.390
be in use by other threads of the same process.

02:11.410 --> 02:17.890
So example of such resources are, for example, heap memory of the process, the open sockets of the

02:17.890 --> 02:24.390
process, the open files, the inter-process communication based techniques that the process is using.

02:24.400 --> 02:31.570
So all these resources which is made available to the process need not be reclaimed back by the operating

02:31.570 --> 02:34.230
system when the thread dies, right?

02:34.240 --> 02:39.970
Because a process could be multithreaded and there could be any number of other threads which may be

02:39.970 --> 02:42.220
using these resources of the process.

02:42.640 --> 02:47.860
So operating system don't have to clean up every resource whenever the thread dies.

02:47.860 --> 02:54.040
So it simply means that operating system don't have to work too hard when a thread is created or when

02:54.040 --> 02:55.300
a thread dies.

02:55.300 --> 02:59.880
And it is for this reason that threads are called lightweight processes.

02:59.890 --> 03:00.670
Right?

03:01.360 --> 03:07.480
And finally, because threads are light weighted processes, therefore the context switching which happens

03:07.480 --> 03:13.240
between the threads which belong to the same process that is, for example, thread, t1 and thread

03:13.270 --> 03:16.780
t two are the threads that belong to the same process.

03:16.810 --> 03:23.050
Then the context switching between the threads from T1 to T2 and vice versa is called thread switching

03:23.050 --> 03:27.150
and this thread switching is faster than the process switching.

03:27.160 --> 03:31.930
Now process switching is nothing, but it is the context switching between the two threads.

03:32.290 --> 03:35.950
But these two threads belong to the different processes.

03:35.950 --> 03:36.640
Right.

03:37.240 --> 03:43.210
So note the difference between thread switching and process Switching and thread switching is way faster

03:43.210 --> 03:44.690
than process switching.

03:44.710 --> 03:51.070
So I would suggest that Google these terms and read more about these terms and understand that why thread

03:51.070 --> 03:53.350
switching is faster than process switching.

03:53.350 --> 03:56.260
The answer lies in these two points.

03:56.260 --> 03:56.920
Right?

03:57.840 --> 04:02.220
The answer lies in the fact that threads are lightweight processes.
