So guys, now let us discuss that.
If there are any number of threads of a process, then what are the resources?
All those threads share amongst themselves.
So it is also important that we have a conceptual understanding regarding how threads are managed by
the operating system.
Right?
So to start with, an operating system allocates resources to the threads.
Those resources could be memory, it could be CPU, it could be access to the hardware, right?
So if your process has a number of threads, then any thread can demand dynamic memory allocation from
the operating system using malloc and clocks.
Right.
Then it is the responsibility of the operating system also to schedule each of the threads on the CPU.
Right.
Of course, several threads will going to be subjected to context switching, but at the end of the
day it is the scheduling policy of the operating system which will going to decide that, which thread
will going to be allocated, which CPU of the system.
And of course there could be any number of threads which can access peripherals or hardware of the system.
Right?
So it is the responsibility of the operating system to allocate resources to the threads.
All threads are siblings.
Note the word siblings.
There is absolutely no parent child relationship or hierarchy between the threads.
Now, in the previous lecture videos I was addressing thread as primary or child threads.
Right?
And now here I am saying that the thread do not have any parent child relationship.
Now note that I was addressing threads as parent and child threads only to imply that which thread created
which thread.
Right.
Other than that, there is absolutely no parent child relationship between the threads where the parent
child relationship means that a parent thread having extra privileges over child thread.
Right?
For example, there is no such thing like when parent thread dies, then all the child thread of that
parent dies.
There is no such thing.
Every thread has its own separate, isolated life cycle.
If parent thread dies, then there is absolutely no impact on the life of a child thread.
So it simply means that an operating system view all threads as siblings.
So it simply means that every thread has its own life cycle, every thread take a birth, every thread
live, and then every thread eventually dies out and then every thread dies out after it has completed
its job and it dies independent of other threads in the system.
Right?
So, however, there is one exception rule which violates this point, and that rule was that that when
the main thread dies, then all other threads of the process are also terminated.
Right.
Given the fact that this main thread was not terminated using p thread underscore exit API.
So we have already discussed this point in the previous lecture video.
Right.
So in the remaining section of this course, whenever I say that thread, one is the parent and thread
T2 is the child.
It's only means that thread t1 created thread t2 right.
It simply do not imply that thread t1 has some extra privileges over thread t2.
Right.
Now multiple threads of the process share the same virtual address space of the process.
Right.
It simply means that if one thread t1 of a process has an address zero x for for example, then this
virtual address.
If made accessible to other thread of the process t2, then the thread T2 can also read and write into
the memory at address for right.
In other words, if there is a common data structure of the process D, then as long as the address
of the data structure D is available to the thread T1 as well as thread T2, then both the threads can
have equal access to this data structure.
D Right.
So threads of the same process among themselves share the heap memory of the process.
They share the common resources such as sockets, the open file descriptors, the global variables.
Right?
So this is a very common interview questions that you can be asked that what are the resources of the
same process which can be shared by all the threads of that process?
So the answer is heap memory sockets, file descriptors, global variables, etcetera.
Right.
What threads of the same process cannot share is the stack memory.
Right?
Every thread of the same process has its own mutually exclusive stack memory.
A31 has its own stack memory and a thread T2 has its own stack memory.
A thread T1 has no access to the stack memory of the thread T2 and thread.
T2 has no access to the stack memory of thread T1.
The stack memory is something which is very private to the threads.
So in the next lecture video, I have added a lecture video from my other course which explains further
details about how stack memory is managed between threads of the same process.
So this is very important from an interview perspective that you are often asked that what are the resources
which are shared among the threads of the same process?
So these are the resources and what is the resource which is not shared between the threads of the same
process.
So such a resource is a stack memory, right?