WEBVTT

00:04.790 --> 00:07.190
So guys, now let us discuss that.

00:07.190 --> 00:11.420
If there are any number of threads of a process, then what are the resources?

00:11.450 --> 00:14.600
All those threads share amongst themselves.

00:15.560 --> 00:21.590
So it is also important that we have a conceptual understanding regarding how threads are managed by

00:21.590 --> 00:22.760
the operating system.

00:22.760 --> 00:23.420
Right?

00:23.420 --> 00:27.920
So to start with, an operating system allocates resources to the threads.

00:27.950 --> 00:33.530
Those resources could be memory, it could be CPU, it could be access to the hardware, right?

00:34.240 --> 00:39.970
So if your process has a number of threads, then any thread can demand dynamic memory allocation from

00:39.970 --> 00:42.400
the operating system using malloc and clocks.

00:42.400 --> 00:43.120
Right.

00:43.480 --> 00:49.060
Then it is the responsibility of the operating system also to schedule each of the threads on the CPU.

00:49.090 --> 00:49.840
Right.

00:50.410 --> 00:55.330
Of course, several threads will going to be subjected to context switching, but at the end of the

00:55.330 --> 01:01.510
day it is the scheduling policy of the operating system which will going to decide that, which thread

01:01.510 --> 01:04.510
will going to be allocated, which CPU of the system.

01:06.250 --> 01:11.890
And of course there could be any number of threads which can access peripherals or hardware of the system.

01:11.890 --> 01:12.580
Right?

01:13.200 --> 01:18.570
So it is the responsibility of the operating system to allocate resources to the threads.

01:18.870 --> 01:21.060
All threads are siblings.

01:21.090 --> 01:22.680
Note the word siblings.

01:22.680 --> 01:27.780
There is absolutely no parent child relationship or hierarchy between the threads.

01:27.810 --> 01:32.850
Now, in the previous lecture videos I was addressing thread as primary or child threads.

01:32.850 --> 01:33.450
Right?

01:33.450 --> 01:37.650
And now here I am saying that the thread do not have any parent child relationship.

01:38.100 --> 01:46.110
Now note that I was addressing threads as parent and child threads only to imply that which thread created

01:46.110 --> 01:46.950
which thread.

01:46.950 --> 01:47.670
Right.

01:49.620 --> 01:55.590
Other than that, there is absolutely no parent child relationship between the threads where the parent

01:55.590 --> 02:00.840
child relationship means that a parent thread having extra privileges over child thread.

02:00.870 --> 02:01.620
Right?

02:01.650 --> 02:07.680
For example, there is no such thing like when parent thread dies, then all the child thread of that

02:07.680 --> 02:08.430
parent dies.

02:08.430 --> 02:09.690
There is no such thing.

02:09.720 --> 02:14.070
Every thread has its own separate, isolated life cycle.

02:14.130 --> 02:19.830
If parent thread dies, then there is absolutely no impact on the life of a child thread.

02:20.280 --> 02:24.960
So it simply means that an operating system view all threads as siblings.

02:24.960 --> 02:31.200
So it simply means that every thread has its own life cycle, every thread take a birth, every thread

02:31.200 --> 02:39.090
live, and then every thread eventually dies out and then every thread dies out after it has completed

02:39.090 --> 02:43.260
its job and it dies independent of other threads in the system.

02:43.260 --> 02:44.070
Right?

02:45.800 --> 02:51.740
So, however, there is one exception rule which violates this point, and that rule was that that when

02:51.740 --> 02:57.200
the main thread dies, then all other threads of the process are also terminated.

02:57.200 --> 02:57.920
Right.

02:58.100 --> 03:04.010
Given the fact that this main thread was not terminated using p thread underscore exit API.

03:04.280 --> 03:07.550
So we have already discussed this point in the previous lecture video.

03:07.550 --> 03:08.240
Right.

03:10.190 --> 03:15.140
So in the remaining section of this course, whenever I say that thread, one is the parent and thread

03:15.170 --> 03:16.040
T2 is the child.

03:16.040 --> 03:20.810
It's only means that thread t1 created thread t2 right.

03:20.810 --> 03:26.500
It simply do not imply that thread t1 has some extra privileges over thread t2.

03:26.510 --> 03:27.260
Right.

03:28.010 --> 03:33.920
Now multiple threads of the process share the same virtual address space of the process.

03:33.920 --> 03:34.550
Right.

03:34.580 --> 03:44.120
It simply means that if one thread t1 of a process has an address zero x for for example, then this

03:44.120 --> 03:45.200
virtual address.

03:45.200 --> 03:52.070
If made accessible to other thread of the process t2, then the thread T2 can also read and write into

03:52.070 --> 03:55.820
the memory at address for right.

03:56.570 --> 04:03.200
In other words, if there is a common data structure of the process D, then as long as the address

04:03.200 --> 04:09.110
of the data structure D is available to the thread T1 as well as thread T2, then both the threads can

04:09.110 --> 04:11.390
have equal access to this data structure.

04:11.390 --> 04:12.800
D Right.

04:13.400 --> 04:18.550
So threads of the same process among themselves share the heap memory of the process.

04:18.560 --> 04:24.890
They share the common resources such as sockets, the open file descriptors, the global variables.

04:24.890 --> 04:25.520
Right?

04:25.520 --> 04:31.100
So this is a very common interview questions that you can be asked that what are the resources of the

04:31.100 --> 04:35.570
same process which can be shared by all the threads of that process?

04:35.570 --> 04:40.910
So the answer is heap memory sockets, file descriptors, global variables, etcetera.

04:40.940 --> 04:41.750
Right.

04:41.840 --> 04:46.280
What threads of the same process cannot share is the stack memory.

04:46.310 --> 04:47.170
Right?

04:47.180 --> 04:53.420
Every thread of the same process has its own mutually exclusive stack memory.

04:53.660 --> 05:00.030
A31 has its own stack memory and a thread T2 has its own stack memory.

05:00.060 --> 05:05.160
A thread T1 has no access to the stack memory of the thread T2 and thread.

05:05.190 --> 05:08.790
T2 has no access to the stack memory of thread T1.

05:09.210 --> 05:13.830
The stack memory is something which is very private to the threads.

05:15.830 --> 05:21.740
So in the next lecture video, I have added a lecture video from my other course which explains further

05:21.740 --> 05:27.000
details about how stack memory is managed between threads of the same process.

05:27.020 --> 05:33.080
So this is very important from an interview perspective that you are often asked that what are the resources

05:33.080 --> 05:36.270
which are shared among the threads of the same process?

05:36.290 --> 05:41.690
So these are the resources and what is the resource which is not shared between the threads of the same

05:41.690 --> 05:42.140
process.

05:42.140 --> 05:45.290
So such a resource is a stack memory, right?
